(byte-compile-nogroup-warn): Warn only when name to be defined is quoted.
[bpt/emacs.git] / lisp / emacs-lisp / lisp-mode.el
CommitLineData
55535639 1;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
6594deb0 2
f9475d97 3;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003, 2004, 2005
cd9d9561 4;; Free Software Foundation, Inc.
3a801d0c 5
e5167999 6;; Maintainer: FSF
fd7fa35a 7;; Keywords: lisp, languages
e5167999 8
a90256cc
BP
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
a90256cc
BP
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
a90256cc 25
e41b2db1
ER
26;;; Commentary:
27
28;; The base major mode for editing Lisp code (used also for Emacs Lisp).
535eadac 29;; This mode is documented in the Emacs manual.
e41b2db1
ER
30
31;;; Code:
32
535eadac 33(defvar lisp-mode-abbrev-table nil)
a90256cc 34
535eadac
DL
35(defvar emacs-lisp-mode-syntax-table
36 (let ((table (make-syntax-table)))
a90256cc 37 (let ((i 0))
a90256cc 38 (while (< i ?0)
535eadac 39 (modify-syntax-entry i "_ " table)
a90256cc
BP
40 (setq i (1+ i)))
41 (setq i (1+ ?9))
42 (while (< i ?A)
535eadac 43 (modify-syntax-entry i "_ " table)
a90256cc
BP
44 (setq i (1+ i)))
45 (setq i (1+ ?Z))
46 (while (< i ?a)
535eadac 47 (modify-syntax-entry i "_ " table)
a90256cc
BP
48 (setq i (1+ i)))
49 (setq i (1+ ?z))
50 (while (< i 128)
535eadac 51 (modify-syntax-entry i "_ " table)
a90256cc 52 (setq i (1+ i)))
535eadac
DL
53 (modify-syntax-entry ? " " table)
54 (modify-syntax-entry ?\t " " table)
55 (modify-syntax-entry ?\f " " table)
56 (modify-syntax-entry ?\n "> " table)
910762b4 57 ;; Give CR the same syntax as newline, for selective-display.
535eadac
DL
58 (modify-syntax-entry ?\^m "> " table)
59 (modify-syntax-entry ?\; "< " table)
60 (modify-syntax-entry ?` "' " table)
61 (modify-syntax-entry ?' "' " table)
62 (modify-syntax-entry ?, "' " table)
592060ab 63 (modify-syntax-entry ?@ "' " table)
a90256cc 64 ;; Used to be singlequote; changed for flonums.
535eadac
DL
65 (modify-syntax-entry ?. "_ " table)
66 (modify-syntax-entry ?# "' " table)
67 (modify-syntax-entry ?\" "\" " table)
68 (modify-syntax-entry ?\\ "\\ " table)
69 (modify-syntax-entry ?\( "() " table)
70 (modify-syntax-entry ?\) ")( " table)
71 (modify-syntax-entry ?\[ "(] " table)
1d3529b6 72 (modify-syntax-entry ?\] ")[ " table))
535eadac
DL
73 table))
74
75(defvar lisp-mode-syntax-table
76 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
535eadac
DL
77 (modify-syntax-entry ?\[ "_ " table)
78 (modify-syntax-entry ?\] "_ " table)
79 (modify-syntax-entry ?# "' 14bn" table)
e5f597f0 80 (modify-syntax-entry ?| "\" 23b" table)
535eadac 81 table))
ecca85de 82
a90256cc
BP
83(define-abbrev-table 'lisp-mode-abbrev-table ())
84
6c2cf866 85(defvar lisp-imenu-generic-expression
535eadac 86 (list
ca2ddd8e 87 (list nil
d7f519ed
GM
88 (purecopy (concat "^\\s-*("
89 (eval-when-compile
90 (regexp-opt
91 '("defun" "defun*" "defsubst" "defmacro"
92 "defadvice" "define-skeleton"
93 "define-minor-mode" "define-derived-mode"
48621281 94 "define-generic-mode"
d7f519ed
GM
95 "define-compiler-macro" "define-modify-macro"
96 "defsetf" "define-setf-expander"
97 "define-method-combination"
63b74e64
SM
98 "defgeneric" "defmethod") t))
99 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
d7f519ed 100 2)
ca2ddd8e 101 (list (purecopy "Variables")
d7f519ed
GM
102 (purecopy (concat "^\\s-*("
103 (eval-when-compile
104 (regexp-opt
105 '("defvar" "defconst" "defconstant" "defcustom"
63b74e64
SM
106 "defparameter" "define-symbol-macro") t))
107 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
d7f519ed 108 2)
ca2ddd8e 109 (list (purecopy "Types")
d7f519ed
GM
110 (purecopy (concat "^\\s-*("
111 (eval-when-compile
112 (regexp-opt
e2cd29bd
JPW
113 '("defgroup" "deftheme" "deftype" "defstruct"
114 "defclass" "define-condition" "define-widget"
115 "defface" "defpackage") t))
63b74e64 116 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
6c2cf866
KH
117 2))
118
119 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
120
d7f519ed
GM
121;; This was originally in autoload.el and is still used there.
122(put 'autoload 'doc-string-elt 3)
123(put 'defun 'doc-string-elt 3)
124(put 'defun* 'doc-string-elt 3)
125(put 'defvar 'doc-string-elt 3)
126(put 'defcustom 'doc-string-elt 3)
e2cd29bd 127(put 'deftheme 'doc-string-elt 2)
d7f519ed
GM
128(put 'defconst 'doc-string-elt 3)
129(put 'defmacro 'doc-string-elt 3)
52c9b141 130(put 'defmacro* 'doc-string-elt 3)
d7f519ed
GM
131(put 'defsubst 'doc-string-elt 3)
132(put 'define-skeleton 'doc-string-elt 2)
133(put 'define-derived-mode 'doc-string-elt 4)
2b0e738a 134(put 'define-compilation-mode 'doc-string-elt 3)
d7f519ed
GM
135(put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
136(put 'define-minor-mode 'doc-string-elt 2)
137(put 'define-generic-mode 'doc-string-elt 7)
138;; define-global-mode has no explicit docstring.
139(put 'easy-mmode-define-global-mode 'doc-string-elt 0)
e2cd29bd 140(put 'define-ibuffer-filter 'doc-string-elt 2)
f767db5b 141(put 'define-ibuffer-op 'doc-string-elt 3)
e2cd29bd 142(put 'define-ibuffer-sorter 'doc-string-elt 2)
d7f519ed
GM
143
144(defun lisp-font-lock-syntactic-face-function (state)
145 (if (nth 3 state)
146 (if (and (eq (nth 0 state) 1)
147 ;; This might be a docstring.
148 (save-excursion
149 (let ((n 0))
150 (goto-char (nth 8 state))
151 (condition-case nil
c689a61d
SM
152 (while (and (not (bobp))
153 (progn (backward-sexp 1) (setq n (1+ n)))))
d7f519ed
GM
154 (scan-error nil))
155 (when (> n 0)
156 (let ((sym (intern-soft
157 (buffer-substring
158 (point) (progn (forward-sexp 1) (point))))))
159 (eq n (or (get sym 'doc-string-elt) 3)))))))
160 font-lock-doc-face
161 font-lock-string-face)
162 font-lock-comment-face))
163
164;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
165;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
166(defun lisp-mode-variables (&optional lisp-syntax)
63b74e64
SM
167 (when lisp-syntax
168 (set-syntax-table lisp-mode-syntax-table))
a90256cc 169 (setq local-abbrev-table lisp-mode-abbrev-table)
a90256cc
BP
170 (make-local-variable 'paragraph-ignore-fill-prefix)
171 (setq paragraph-ignore-fill-prefix t)
35d132a8
RS
172 (make-local-variable 'fill-paragraph-function)
173 (setq fill-paragraph-function 'lisp-fill-paragraph)
3272c162
RS
174 ;; Adaptive fill mode gets in the way of auto-fill,
175 ;; and should make no difference for explicit fill
176 ;; because lisp-fill-paragraph should do the job.
63b74e64
SM
177 ;; I believe that newcomment's auto-fill code properly deals with it -stef
178 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
a90256cc
BP
179 (make-local-variable 'indent-line-function)
180 (setq indent-line-function 'lisp-indent-line)
181 (make-local-variable 'indent-region-function)
182 (setq indent-region-function 'lisp-indent-region)
183 (make-local-variable 'parse-sexp-ignore-comments)
184 (setq parse-sexp-ignore-comments t)
5847f861 185 (make-local-variable 'outline-regexp)
43817a75 186 (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
a8050bff
GM
187 (make-local-variable 'outline-level)
188 (setq outline-level 'lisp-outline-level)
a90256cc
BP
189 (make-local-variable 'comment-start)
190 (setq comment-start ";")
191 (make-local-variable 'comment-start-skip)
e56a043b
MB
192 ;; Look within the line for a ; following an even number of backslashes
193 ;; after either a non-backslash or the line beginning.
194 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
76f41479
RS
195 (make-local-variable 'font-lock-comment-start-skip)
196 ;; Font lock mode uses this only when it KNOWS a comment is starting.
197 (setq font-lock-comment-start-skip ";+ *")
3f0c3d8b
SM
198 (make-local-variable 'comment-add)
199 (setq comment-add 1) ;default to `;;' in comment-region
a90256cc
BP
200 (make-local-variable 'comment-column)
201 (setq comment-column 40)
d8a8cbe2
SM
202 ;; Don't get confused by `;' in doc strings when paragraph-filling.
203 (set (make-local-variable 'comment-use-global-state) t)
6c2cf866 204 (make-local-variable 'imenu-generic-expression)
1d3529b6
KH
205 (setq imenu-generic-expression lisp-imenu-generic-expression)
206 (make-local-variable 'multibyte-syntax-as-symbol)
1594a23a 207 (setq multibyte-syntax-as-symbol t)
52cf5c37 208 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
1594a23a
SM
209 (setq font-lock-defaults
210 '((lisp-font-lock-keywords
211 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
1b1556b1 212 nil nil (("+-*/.<>=!?$%_&~^:@" . "w")) nil
d7f519ed
GM
213 (font-lock-mark-block-function . mark-defun)
214 (font-lock-syntactic-face-function
215 . lisp-font-lock-syntactic-face-function))))
a8050bff
GM
216
217(defun lisp-outline-level ()
218 "Lisp mode `outline-level' function."
43817a75
LK
219 (let ((len (- (match-end 0) (match-beginning 0))))
220 (if (looking-at "(\\|;;;###autoload")
221 1000
222 len)))
ca2ddd8e 223
1594a23a
SM
224(defvar lisp-mode-shared-map
225 (let ((map (make-sparse-keymap)))
a25e82a8 226 (define-key map "\t" 'lisp-indent-line)
1594a23a
SM
227 (define-key map "\e\C-q" 'indent-sexp)
228 (define-key map "\177" 'backward-delete-char-untabify)
c1acacc4
EZ
229 ;; This gets in the way when viewing a Lisp file in view-mode. As
230 ;; long as [backspace] is mapped into DEL via the
231 ;; function-key-map, this should remain disabled!!
232 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
1594a23a 233 map)
a90256cc
BP
234 "Keymap for commands shared by all sorts of Lisp modes.")
235
a90256cc
BP
236(defvar emacs-lisp-mode-map ()
237 "Keymap for Emacs Lisp mode.
99ec65b3 238All commands in `lisp-mode-shared-map' are inherited by this map.")
a90256cc
BP
239
240(if emacs-lisp-mode-map
241 ()
b3a3cb63 242 (let ((map (make-sparse-keymap "Emacs-Lisp")))
b8bc6df2 243 (setq emacs-lisp-mode-map (make-sparse-keymap))
99ec65b3 244 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
b3a3cb63
KH
245 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
246 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
889bfc7d 247 (define-key emacs-lisp-mode-map "\e\C-q" 'indent-pp-sexp)
b3a3cb63
KH
248 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap))
249 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp]
250 (cons "Emacs-Lisp" map))
251 (define-key map [edebug-defun]
252 '("Instrument Function for Debugging" . edebug-defun))
253 (define-key map [byte-recompile]
254 '("Byte-recompile Directory..." . byte-recompile-directory))
00e40cc9 255 (define-key map [emacs-byte-compile-and-load]
eaec854f 256 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
b3a3cb63
KH
257 (define-key map [byte-compile]
258 '("Byte-compile This File" . emacs-lisp-byte-compile))
259 (define-key map [separator-eval] '("--"))
260 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer))
261 (define-key map [eval-region] '("Evaluate Region" . eval-region))
262 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp))
263 (define-key map [separator-format] '("--"))
264 (define-key map [comment-region] '("Comment Out Region" . comment-region))
265 (define-key map [indent-region] '("Indent Region" . indent-region))
4b619eca
SM
266 (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
267 (put 'eval-region 'menu-enable 'mark-active)
268 (put 'comment-region 'menu-enable 'mark-active)
269 (put 'indent-region 'menu-enable 'mark-active)))
b3a3cb63
KH
270
271(defun emacs-lisp-byte-compile ()
272 "Byte compile the file containing the current buffer."
273 (interactive)
274 (if buffer-file-name
275 (byte-compile-file buffer-file-name)
767a1151
KH
276 (error "The buffer must be saved in a file first")))
277
eaec854f 278(defun emacs-lisp-byte-compile-and-load ()
767a1151
KH
279 "Byte-compile the current file (if it has changed), then load compiled code."
280 (interactive)
281 (or buffer-file-name
282 (error "The buffer must be saved in a file first"))
283 (require 'bytecomp)
284 ;; Recompile if file or buffer has changed since last compilation.
eaec854f 285 (if (and (buffer-modified-p)
535eadac 286 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
eaec854f
SM
287 (save-buffer))
288 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
289 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
290 (load-file compiled-file-name)
291 (byte-compile-file buffer-file-name t))))
a90256cc 292
e596094d
DL
293(defcustom emacs-lisp-mode-hook nil
294 "Hook run when entering Emacs Lisp mode."
535eadac 295 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
e596094d
DL
296 :type 'hook
297 :group 'lisp)
298
299(defcustom lisp-mode-hook nil
300 "Hook run when entering Lisp mode."
301 :options '(imenu-add-menubar-index)
302 :type 'hook
303 :group 'lisp)
304
305(defcustom lisp-interaction-mode-hook nil
306 "Hook run when entering Lisp Interaction mode."
307 :options '(turn-on-eldoc-mode)
308 :type 'hook
309 :group 'lisp)
310
dda7c010 311(defun emacs-lisp-mode ()
a90256cc
BP
312 "Major mode for editing Lisp code to run in Emacs.
313Commands:
314Delete converts tabs to spaces as it moves back.
315Blank lines separate paragraphs. Semicolons start comments.
316\\{emacs-lisp-mode-map}
317Entry to this mode calls the value of `emacs-lisp-mode-hook'
318if that value is non-nil."
dda7c010
RS
319 (interactive)
320 (kill-all-local-variables)
321 (use-local-map emacs-lisp-mode-map)
322 (set-syntax-table emacs-lisp-mode-syntax-table)
323 (setq major-mode 'emacs-lisp-mode)
324 (setq mode-name "Emacs-Lisp")
d7f519ed 325 (lisp-mode-variables)
dda7c010 326 (setq imenu-case-fold-search nil)
fb16e932 327 (run-mode-hooks 'emacs-lisp-mode-hook))
c689a61d 328(put 'emacs-lisp-mode 'custom-mode-group 'lisp)
a90256cc 329
535eadac
DL
330(defvar lisp-mode-map
331 (let ((map (make-sparse-keymap)))
99ec65b3 332 (set-keymap-parent map lisp-mode-shared-map)
535eadac
DL
333 (define-key map "\e\C-x" 'lisp-eval-defun)
334 (define-key map "\C-c\C-z" 'run-lisp)
335 map)
a90256cc 336 "Keymap for ordinary Lisp mode.
99ec65b3 337All commands in `lisp-mode-shared-map' are inherited by this map.")
a90256cc 338
dda7c010 339(defun lisp-mode ()
a90256cc
BP
340 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
341Commands:
342Delete converts tabs to spaces as it moves back.
343Blank lines separate paragraphs. Semicolons start comments.
344\\{lisp-mode-map}
345Note that `run-lisp' may be used either to start an inferior Lisp job
346or to switch back to an existing one.
347
348Entry to this mode calls the value of `lisp-mode-hook'
349if that value is non-nil."
dda7c010
RS
350 (interactive)
351 (kill-all-local-variables)
352 (use-local-map lisp-mode-map)
353 (setq major-mode 'lisp-mode)
354 (setq mode-name "Lisp")
d7f519ed 355 (lisp-mode-variables)
dda7c010
RS
356 (make-local-variable 'comment-start-skip)
357 (setq comment-start-skip
d7f519ed 358 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
dda7c010
RS
359 (make-local-variable 'font-lock-keywords-case-fold-search)
360 (setq font-lock-keywords-case-fold-search t)
361 (setq imenu-case-fold-search t)
362 (set-syntax-table lisp-mode-syntax-table)
fb16e932 363 (run-mode-hooks 'lisp-mode-hook))
59de4ad0
SS
364(put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
365
366(defun lisp-find-tag-default ()
367 (let ((default (find-tag-default)))
368 (when (stringp default)
369 (if (string-match ":+" default)
370 (substring default (match-end 0))
ea2e9f8d 371 default))))
a90256cc 372
60b2e60d
DL
373;; Used in old LispM code.
374(defalias 'common-lisp-mode 'lisp-mode)
375
5e871da0
DL
376;; This will do unless inf-lisp.el is loaded.
377(defun lisp-eval-defun (&optional and-go)
a90256cc
BP
378 "Send the current defun to the Lisp process made by \\[run-lisp]."
379 (interactive)
380 (error "Process lisp does not exist"))
381
535eadac
DL
382(defvar lisp-interaction-mode-map
383 (let ((map (make-sparse-keymap)))
99ec65b3 384 (set-keymap-parent map lisp-mode-shared-map)
535eadac 385 (define-key map "\e\C-x" 'eval-defun)
889bfc7d 386 (define-key map "\e\C-q" 'indent-pp-sexp)
535eadac
DL
387 (define-key map "\e\t" 'lisp-complete-symbol)
388 (define-key map "\n" 'eval-print-last-sexp)
389 map)
bacd83a7 390 "Keymap for Lisp Interaction mode.
99ec65b3 391All commands in `lisp-mode-shared-map' are inherited by this map.")
a90256cc 392
52cf5c37 393(defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
1594a23a 394(define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
a90256cc
BP
395 "Major mode for typing and evaluating Lisp forms.
396Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
397before point, and prints its value into the buffer, advancing point.
c9be79b0 398Note that printing is controlled by `eval-expression-print-length'
94f8f5d3 399and `eval-expression-print-level'.
a90256cc
BP
400
401Commands:
402Delete converts tabs to spaces as it moves back.
403Paragraphs are separated only by blank lines.
404Semicolons start comments.
405\\{lisp-interaction-mode-map}
406Entry to this mode calls the value of `lisp-interaction-mode-hook'
52cf5c37 407if that value is non-nil.")
a90256cc 408
121f0d57 409(defun eval-print-last-sexp ()
343462ed
EZ
410 "Evaluate sexp before point; print value into current buffer.
411
412Note that printing the result is controlled by the variables
413`eval-expression-print-length' and `eval-expression-print-level',
414which see."
121f0d57 415 (interactive)
f798d950
JB
416 (let ((standard-output (current-buffer)))
417 (terpri)
418 (eval-last-sexp t)
419 (terpri)))
ca2ddd8e 420
5f096255 421
cb79ea64
GM
422(defun last-sexp-setup-props (beg end value alt1 alt2)
423 "Set up text properties for the output of `eval-last-sexp-1'.
424BEG and END are the start and end of the output in current-buffer.
a1506d29 425VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
cb79ea64
GM
426alternative printed representations that can be displayed."
427 (let ((map (make-sparse-keymap)))
428 (define-key map "\C-m" 'last-sexp-toggle-display)
429 (define-key map [down-mouse-2] 'mouse-set-point)
430 (define-key map [mouse-2] 'last-sexp-toggle-display)
431 (add-text-properties
a1506d29 432 beg end
cb79ea64 433 `(printed-value (,value ,alt1 ,alt2)
a1506d29 434 mouse-face highlight
cb79ea64
GM
435 keymap ,map
436 help-echo "RET, mouse-2: toggle abbreviated display"
437 rear-nonsticky (mouse-face keymap help-echo
438 printed-value)))))
439
440
98996d89 441(defun last-sexp-toggle-display (&optional arg)
cb79ea64 442 "Toggle between abbreviated and unabbreviated printed representations."
98996d89 443 (interactive "P")
3b8d36f1
RS
444 (save-restriction
445 (widen)
98996d89
RS
446 (let ((value (get-text-property (point) 'printed-value)))
447 (when value
448 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
449 'printed-value)
450 (point)))
451 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
452 (standard-output (current-buffer))
453 (point (point)))
454 (delete-region beg end)
455 (insert (nth 1 value))
456 (last-sexp-setup-props beg (point)
457 (nth 0 value)
458 (nth 2 value)
459 (nth 1 value))
460 (goto-char (min (point-max) point)))))))
5f096255 461
c689a61d
SM
462(defun prin1-char (char)
463 "Return a string representing CHAR as a character rather than as an integer.
464If CHAR is not a character, return nil."
465 (and (integerp char)
7a439904 466 (eventp char)
87fdf320 467 (let ((c (event-basic-type char))
4f4ce597
RS
468 (mods (event-modifiers char))
469 string)
87fdf320
RS
470 ;; Prevent ?A from turning into ?\S-a.
471 (if (and (memq 'shift mods)
4f4ce597 472 (zerop (logand char ?\S-\^@))
87fdf320
RS
473 (not (let ((case-fold-search nil))
474 (char-equal c (upcase c)))))
475 (setq c (upcase c) mods nil))
4f4ce597
RS
476 ;; What string are we considering using?
477 (condition-case nil
478 (setq string
479 (concat
480 "?"
481 (mapconcat
482 (lambda (modif)
483 (cond ((eq modif 'super) "\\s-")
484 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
485 mods "")
486 (cond
487 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
488 ((eq c 127) "\\C-?")
489 (t
490 (string c)))))
491 (error nil))
492 ;; Verify the string reads a CHAR, not to some other character.
493 ;; If it doesn't, return nil instead.
494 (and string
495 (= (car (read-from-string string)) char)
496 string))))
2b0e738a 497
c689a61d 498
99c6d63b 499(defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
a90256cc
BP
500 "Evaluate sexp before point; print value in minibuffer.
501With argument, print output into current buffer."
99c6d63b 502 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
efb195f0
RS
503 (let ((value
504 (eval (let ((stab (syntax-table))
361721f2 505 (opoint (point))
05e94d32 506 ignore-quotes
361721f2 507 expr)
c4e2d791
RS
508 (save-excursion
509 (with-syntax-table emacs-lisp-mode-syntax-table
510 ;; If this sexp appears to be enclosed in `...'
511 ;; then ignore the surrounding quotes.
512 (setq ignore-quotes
513 (or (eq (following-char) ?\')
514 (eq (preceding-char) ?\')))
515 (forward-sexp -1)
516 ;; If we were after `?\e' (or similar case),
517 ;; use the whole thing, not just the `e'.
518 (when (eq (preceding-char) ?\\)
519 (forward-char -1)
520 (when (eq (preceding-char) ??)
521 (forward-char -1)))
522
523 ;; Skip over `#N='s.
524 (when (eq (preceding-char) ?=)
525 (let (labeled-p)
526 (save-excursion
527 (skip-chars-backward "0-9#=")
528 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
529 (when labeled-p
530 (forward-sexp -1))))
531
532 (save-restriction
533 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
534 ;; `variable' so that the value is returned, not the
535 ;; name
536 (if (and ignore-quotes
537 (eq (following-char) ?`))
538 (forward-char))
539 (narrow-to-region (point-min) opoint)
540 (setq expr (read (current-buffer)))
541 ;; If it's an (interactive ...) form, it's more
542 ;; useful to show how an interactive call would
543 ;; use it.
544 (and (consp expr)
545 (eq (car expr) 'interactive)
546 (setq expr
547 (list 'call-interactively
548 (list 'quote
549 (list 'lambda
550 '(&rest args)
551 expr
552 'args)))))
553 expr)))))))
606f5e75
RS
554 (eval-last-sexp-print-value value))))
555
556(defun eval-last-sexp-print-value (value)
557 (let ((unabbreviated (let ((print-length nil) (print-level nil))
558 (prin1-to-string value)))
559 (print-length eval-expression-print-length)
560 (print-level eval-expression-print-level)
606f5e75
RS
561 (beg (point))
562 end)
563 (prog1
564 (prin1 value)
152472ba
RS
565 (let ((str (eval-expression-print-format value)))
566 (if str (princ str)))
606f5e75
RS
567 (setq end (point))
568 (when (and (bufferp standard-output)
569 (or (not (null print-length))
570 (not (null print-level)))
571 (not (string= unabbreviated
572 (buffer-substring-no-properties beg end))))
573 (last-sexp-setup-props beg end value
574 unabbreviated
575 (buffer-substring-no-properties beg end))
576 ))))
5f096255 577
a90256cc 578
06f308a7
RS
579(defvar eval-last-sexp-fake-value (make-symbol "t"))
580
99c6d63b
GM
581(defun eval-last-sexp (eval-last-sexp-arg-internal)
582 "Evaluate sexp before point; print value in minibuffer.
0202508f 583Interactively, with prefix argument, print output into current buffer."
99c6d63b
GM
584 (interactive "P")
585 (if (null eval-expression-debug-on-error)
586 (eval-last-sexp-1 eval-last-sexp-arg-internal)
06f308a7 587 (let ((old-value eval-last-sexp-fake-value) new-value value)
99c6d63b
GM
588 (let ((debug-on-error old-value))
589 (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal))
590 (setq new-value debug-on-error))
591 (unless (eq old-value new-value)
592 (setq debug-on-error new-value))
593 value)))
ca2ddd8e 594
151a410a 595(defun eval-defun-1 (form)
217297f8
JL
596 "Treat some expressions specially.
597Reset the `defvar' and `defcustom' variables to the initial value.
598Reinitialize the face according to the `defface' specification."
99ec65b3
DL
599 ;; The code in edebug-defun should be consistent with this, but not
600 ;; the same, since this gets a macroexpended form.
726e8778
RS
601 (cond ((not (listp form))
602 form)
603 ((and (eq (car form) 'defvar)
c689a61d
SM
604 (cdr-safe (cdr-safe form))
605 (boundp (cadr form)))
606 ;; Force variable to be re-set.
607 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
f9475d97 608 (setq-default ,(nth 1 form) ,(nth 2 form))))
5e871da0
DL
609 ;; `defcustom' is now macroexpanded to
610 ;; `custom-declare-variable' with a quoted value arg.
535eadac
DL
611 ((and (eq (car form) 'custom-declare-variable)
612 (default-boundp (eval (nth 1 form))))
151a410a 613 ;; Force variable to be bound.
5e871da0 614 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
151a410a 615 form)
217297f8
JL
616 ;; `defface' is macroexpanded to `custom-declare-face'.
617 ((eq (car form) 'custom-declare-face)
618 ;; Reset the face.
619 (put (eval (nth 1 form)) 'face-defface-spec nil)
620 (setq face-new-frame-defaults
621 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
622 form)
151a410a
RS
623 ((eq (car form) 'progn)
624 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
625 (t form)))
626
105d6be1 627(defun eval-defun-2 ()
121f0d57 628 "Evaluate defun that point is in or before.
ebc03d28
KH
629The value is displayed in the minibuffer.
630If the current defun is actually a call to `defvar',
631then reset the variable using the initial value expression
632even if the variable already has some other value.
633\(Normally `defvar' does not change the variable's value
634if it already has a value.\)
635
2298f9f7
KH
636With argument, insert value in current buffer after the defun.
637Return the result of evaluation."
a90256cc 638 (interactive "P")
efb195f0
RS
639 (let ((debug-on-error eval-expression-debug-on-error)
640 (print-length eval-expression-print-length)
641 (print-level eval-expression-print-level))
642 (save-excursion
643 ;; Arrange for eval-region to "read" the (possibly) altered form.
644 ;; eval-region handles recording which file defines a function or
645 ;; variable. Re-written using `apply' to avoid capturing
646 ;; variables like `end'.
647 (apply
ca2ddd8e 648 #'eval-region
105d6be1 649 (let ((standard-output t)
efb195f0
RS
650 beg end form)
651 ;; Read the form from the buffer, and record where it ends.
652 (save-excursion
653 (end-of-defun)
654 (beginning-of-defun)
655 (setq beg (point))
656 (setq form (read (current-buffer)))
657 (setq end (point)))
217297f8 658 ;; Alter the form if necessary.
efb195f0
RS
659 (setq form (eval-defun-1 (macroexpand form)))
660 (list beg end standard-output
661 `(lambda (ignore)
662 ;; Skipping to the end of the specified region
663 ;; will make eval-region return.
664 (goto-char ,end)
665 ',form))))))
713c3fb1
DL
666 ;; The result of evaluation has been put onto VALUES. So return it.
667 (car values))
99c6d63b 668
105d6be1
GM
669(defun eval-defun (edebug-it)
670 "Evaluate the top-level form containing point, or after point.
99c6d63b 671
46f90d0f
RS
672If the current defun is actually a call to `defvar' or `defcustom',
673evaluating it this way resets the variable using its initial value
674expression even if the variable already has some other value.
675\(Normally `defvar' and `defcustom' do not alter the value if there
676already is one.)
105d6be1
GM
677
678With a prefix argument, instrument the code for Edebug.
679
680If acting on a `defun' for FUNCTION, and the function was
681instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
682instrumented, just FUNCTION is printed.
683
684If not acting on a `defun', the result of evaluation is displayed in
343462ed
EZ
685the minibuffer. This display is controlled by the variables
686`eval-expression-print-length' and `eval-expression-print-level',
687which see."
99c6d63b 688 (interactive "P")
105d6be1
GM
689 (cond (edebug-it
690 (require 'edebug)
691 (eval-defun (not edebug-all-defs)))
692 (t
693 (if (null eval-expression-debug-on-error)
694 (eval-defun-2)
695 (let ((old-value (make-symbol "t")) new-value value)
696 (let ((debug-on-error old-value))
697 (setq value (eval-defun-2))
698 (setq new-value debug-on-error))
699 (unless (eq old-value new-value)
700 (setq debug-on-error new-value))
701 value)))))
98996d89 702\f
ca2ddd8e 703
a90256cc
BP
704(defun lisp-comment-indent ()
705 (if (looking-at "\\s<\\s<\\s<")
706 (current-column)
707 (if (looking-at "\\s<\\s<")
531cbff1 708 (let ((tem (or (calculate-lisp-indent) (current-column))))
a90256cc
BP
709 (if (listp tem) (car tem) tem))
710 (skip-chars-backward " \t")
711 (max (if (bolp) 0 (1+ (current-column)))
712 comment-column))))
713
63b74e64
SM
714;; This function just forces a more costly detection of comments (using
715;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
716;; taking a `;' inside a string started on another line for a comment starter.
4f9d8764
SM
717;; Note: `newcomment' gets it right now since we set comment-use-global-state
718;; so we could get rid of it. -stef
7eb67d79
RS
719(defun lisp-mode-auto-fill ()
720 (if (> (current-column) (current-fill-column))
721 (if (save-excursion
52cf5c37 722 (nth 4 (syntax-ppss (point))))
7eb67d79 723 (do-auto-fill)
52cf5c37
SM
724 (unless (and (boundp 'comment-auto-fill-only-comments)
725 comment-auto-fill-only-comments)
726 (let ((comment-start nil) (comment-start-skip nil))
727 (do-auto-fill))))))
7eb67d79 728
cada28bb
EZ
729(defvar lisp-indent-offset nil
730 "If non-nil, indent second line of expressions that many more columns.")
535eadac 731(defvar lisp-indent-function 'lisp-indent-function)
a90256cc
BP
732
733(defun lisp-indent-line (&optional whole-exp)
734 "Indent current line as Lisp code.
735With argument, indent any additional lines of the same expression
736rigidly along with this one."
737 (interactive "P")
d7f519ed
GM
738 (let ((indent (calculate-lisp-indent)) shift-amt end
739 (pos (- (point-max) (point)))
740 (beg (progn (beginning-of-line) (point))))
a90256cc 741 (skip-chars-forward " \t")
531cbff1
RS
742 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
743 ;; Don't alter indentation of a ;;; comment line
744 ;; or a line that starts in a string.
328561fc 745 (goto-char (- (point-max) pos))
a90256cc
BP
746 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
747 ;; Single-semicolon comment lines should be indented
748 ;; as comment lines, not as code.
749 (progn (indent-for-comment) (forward-char -1))
750 (if (listp indent) (setq indent (car indent)))
751 (setq shift-amt (- indent (current-column)))
752 (if (zerop shift-amt)
753 nil
754 (delete-region beg (point))
755 (indent-to indent)))
756 ;; If initial point was within line's indentation,
757 ;; position after the indentation. Else stay at same point in text.
758 (if (> (- (point-max) pos) (point))
759 (goto-char (- (point-max) pos)))
760 ;; If desired, shift remaining lines of expression the same amount.
761 (and whole-exp (not (zerop shift-amt))
762 (save-excursion
763 (goto-char beg)
764 (forward-sexp 1)
765 (setq end (point))
766 (goto-char beg)
767 (forward-line 1)
768 (setq beg (point))
769 (> end beg))
770 (indent-code-rigidly beg end shift-amt)))))
771
22486a7f 772(defvar calculate-lisp-indent-last-sexp)
c0df1d61 773
a90256cc
BP
774(defun calculate-lisp-indent (&optional parse-start)
775 "Return appropriate indentation for current line as Lisp code.
776In usual case returns an integer: the column to indent to.
531cbff1
RS
777If the value is nil, that means don't change the indentation
778because the line starts inside a string.
779
780The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
a90256cc 781This means that following lines at the same level of indentation
531cbff1
RS
782should not necessarily be indented the same as this line.
783Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
784is the buffer position of the start of the containing expression."
a90256cc
BP
785 (save-excursion
786 (beginning-of-line)
787 (let ((indent-point (point))
788 state paren-depth
789 ;; setting this to a number inhibits calling hook
790 (desired-indent nil)
791 (retry t)
c0df1d61 792 calculate-lisp-indent-last-sexp containing-sexp)
a90256cc
BP
793 (if parse-start
794 (goto-char parse-start)
795 (beginning-of-defun))
796 ;; Find outermost containing sexp
797 (while (< (point) indent-point)
798 (setq state (parse-partial-sexp (point) indent-point 0)))
799 ;; Find innermost containing sexp
800 (while (and retry
801 state
802 (> (setq paren-depth (elt state 0)) 0))
803 (setq retry nil)
c0df1d61 804 (setq calculate-lisp-indent-last-sexp (elt state 2))
a90256cc
BP
805 (setq containing-sexp (elt state 1))
806 ;; Position following last unclosed open.
807 (goto-char (1+ containing-sexp))
808 ;; Is there a complete sexp since then?
c0df1d61
RS
809 (if (and calculate-lisp-indent-last-sexp
810 (> calculate-lisp-indent-last-sexp (point)))
a90256cc 811 ;; Yes, but is there a containing sexp after that?
c0df1d61
RS
812 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
813 indent-point 0)))
a90256cc
BP
814 (if (setq retry (car (cdr peek))) (setq state peek)))))
815 (if retry
816 nil
817 ;; Innermost containing sexp found
818 (goto-char (1+ containing-sexp))
c0df1d61 819 (if (not calculate-lisp-indent-last-sexp)
a90256cc
BP
820 ;; indent-point immediately follows open paren.
821 ;; Don't call hook.
822 (setq desired-indent (current-column))
823 ;; Find the start of first element of containing sexp.
c0df1d61 824 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
a90256cc
BP
825 (cond ((looking-at "\\s(")
826 ;; First element of containing sexp is a list.
827 ;; Indent under that list.
828 )
829 ((> (save-excursion (forward-line 1) (point))
c0df1d61 830 calculate-lisp-indent-last-sexp)
a90256cc
BP
831 ;; This is the first line to start within the containing sexp.
832 ;; It's almost certainly a function call.
c0df1d61 833 (if (= (point) calculate-lisp-indent-last-sexp)
a90256cc
BP
834 ;; Containing sexp has nothing before this line
835 ;; except the first element. Indent under that element.
836 nil
837 ;; Skip the first element, find start of second (the first
838 ;; argument of the function call) and indent under.
839 (progn (forward-sexp 1)
c0df1d61
RS
840 (parse-partial-sexp (point)
841 calculate-lisp-indent-last-sexp
842 0 t)))
a90256cc
BP
843 (backward-prefix-chars))
844 (t
c0df1d61 845 ;; Indent beneath first sexp on same line as
535eadac 846 ;; `calculate-lisp-indent-last-sexp'. Again, it's
c0df1d61
RS
847 ;; almost certainly a function call.
848 (goto-char calculate-lisp-indent-last-sexp)
a90256cc 849 (beginning-of-line)
c0df1d61
RS
850 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
851 0 t)
a90256cc
BP
852 (backward-prefix-chars)))))
853 ;; Point is at the point to indent under unless we are inside a string.
eb8c3be9 854 ;; Call indentation hook except when overridden by lisp-indent-offset
a90256cc
BP
855 ;; or if the desired indentation has already been computed.
856 (let ((normal-indent (current-column)))
857 (cond ((elt state 3)
858 ;; Inside a string, don't change indentation.
531cbff1 859 nil)
a90256cc
BP
860 ((and (integerp lisp-indent-offset) containing-sexp)
861 ;; Indent by constant offset
862 (goto-char containing-sexp)
863 (+ (current-column) lisp-indent-offset))
864 (desired-indent)
865 ((and (boundp 'lisp-indent-function)
866 lisp-indent-function
867 (not retry))
868 (or (funcall lisp-indent-function indent-point state)
869 normal-indent))
870 (t
f30ff39f 871 normal-indent))))))
a90256cc
BP
872
873(defun lisp-indent-function (indent-point state)
9fefa08b
RS
874 "This function is the normal value of the variable `lisp-indent-function'.
875It is used when indenting a line within a function call, to see if the
876called function says anything special about how to indent the line.
877
878INDENT-POINT is the position where the user typed TAB, or equivalent.
879Point is located at the point to indent under (for default indentation);
880STATE is the `parse-partial-sexp' state for that position.
881
882If the current line is in a call to a Lisp function
883which has a non-nil property `lisp-indent-function',
884that specifies how to do the indentation. The property value can be
885* `defun', meaning indent `defun'-style;
886* an integer N, meaning indent the first N arguments specially
b961eb0e
TTN
887 like ordinary function arguments and then indent any further
888 arguments like a body;
9fefa08b 889* a function to call just as this function was called.
cc08f5b2
TTN
890 If that function returns nil, that means it doesn't specify
891 the indentation.
9fefa08b
RS
892
893This function also returns nil meaning don't specify the indentation."
a90256cc
BP
894 (let ((normal-indent (current-column)))
895 (goto-char (1+ (elt state 1)))
c0df1d61 896 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
a90256cc
BP
897 (if (and (elt state 2)
898 (not (looking-at "\\sw\\|\\s_")))
4696802b 899 ;; car of form doesn't seem to be a symbol
a90256cc
BP
900 (progn
901 (if (not (> (save-excursion (forward-line 1) (point))
c0df1d61 902 calculate-lisp-indent-last-sexp))
c689a61d
SM
903 (progn (goto-char calculate-lisp-indent-last-sexp)
904 (beginning-of-line)
905 (parse-partial-sexp (point)
906 calculate-lisp-indent-last-sexp 0 t)))
907 ;; Indent under the list or under the first sexp on the same
908 ;; line as calculate-lisp-indent-last-sexp. Note that first
909 ;; thing on that line has to be complete sexp since we are
c0df1d61 910 ;; inside the innermost containing sexp.
a90256cc
BP
911 (backward-prefix-chars)
912 (current-column))
913 (let ((function (buffer-substring (point)
914 (progn (forward-sexp 1) (point))))
915 method)
2dab7802
RS
916 (setq method (or (get (intern-soft function) 'lisp-indent-function)
917 (get (intern-soft function) 'lisp-indent-hook)))
a90256cc
BP
918 (cond ((or (eq method 'defun)
919 (and (null method)
920 (> (length function) 3)
921 (string-match "\\`def" function)))
922 (lisp-indent-defform state indent-point))
923 ((integerp method)
924 (lisp-indent-specform method state
925 indent-point normal-indent))
926 (method
cc08f5b2 927 (funcall method indent-point state)))))))
a90256cc 928
d7fa5aa2 929(defvar lisp-body-indent 2
ab69b2fb 930 "Number of columns to indent the second line of a `(def...)' form.")
a90256cc
BP
931
932(defun lisp-indent-specform (count state indent-point normal-indent)
933 (let ((containing-form-start (elt state 1))
934 (i count)
935 body-indent containing-form-column)
936 ;; Move to the start of containing form, calculate indentation
937 ;; to use for non-distinguished forms (> count), and move past the
938 ;; function symbol. lisp-indent-function guarantees that there is at
939 ;; least one word or symbol character following open paren of containing
940 ;; form.
941 (goto-char containing-form-start)
942 (setq containing-form-column (current-column))
943 (setq body-indent (+ lisp-body-indent containing-form-column))
944 (forward-char 1)
945 (forward-sexp 1)
946 ;; Now find the start of the last form.
947 (parse-partial-sexp (point) indent-point 1 t)
948 (while (and (< (point) indent-point)
949 (condition-case ()
950 (progn
951 (setq count (1- count))
952 (forward-sexp 1)
953 (parse-partial-sexp (point) indent-point 1 t))
954 (error nil))))
955 ;; Point is sitting on first character of last (or count) sexp.
956 (if (> count 0)
957 ;; A distinguished form. If it is the first or second form use double
958 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
959 ;; to 2 (the default), this just happens to work the same with if as
960 ;; the older code, but it makes unwind-protect, condition-case,
961 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
962 ;; less hacked, behavior can be obtained by replacing below with
963 ;; (list normal-indent containing-form-start).
964 (if (<= (- i count) 1)
965 (list (+ containing-form-column (* 2 lisp-body-indent))
966 containing-form-start)
967 (list normal-indent containing-form-start))
968 ;; A non-distinguished form. Use body-indent if there are no
969 ;; distinguished forms and this is the first undistinguished form,
970 ;; or if this is the first undistinguished form and the preceding
971 ;; distinguished form has indentation at least as great as body-indent.
972 (if (or (and (= i 0) (= count 0))
973 (and (= count 0) (<= body-indent normal-indent)))
974 body-indent
975 normal-indent))))
976
977(defun lisp-indent-defform (state indent-point)
978 (goto-char (car (cdr state)))
979 (forward-line 1)
980 (if (> (point) (car (cdr (cdr state))))
981 (progn
982 (goto-char (car (cdr state)))
983 (+ lisp-body-indent (current-column)))))
984
ca2ddd8e 985
a90256cc
BP
986;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
987;; like defun if the first form is placed on the next line, otherwise
988;; it is indented like any other form (i.e. forms line up under first).
989
990(put 'lambda 'lisp-indent-function 'defun)
991(put 'autoload 'lisp-indent-function 'defun)
992(put 'progn 'lisp-indent-function 0)
993(put 'prog1 'lisp-indent-function 1)
994(put 'prog2 'lisp-indent-function 2)
995(put 'save-excursion 'lisp-indent-function 0)
996(put 'save-window-excursion 'lisp-indent-function 0)
4b619eca 997(put 'save-selected-window 'lisp-indent-function 0)
a90256cc 998(put 'save-restriction 'lisp-indent-function 0)
cfe158ab 999(put 'save-match-data 'lisp-indent-function 0)
38f16fe1 1000(put 'save-current-buffer 'lisp-indent-function 0)
54c014f0 1001(put 'with-current-buffer 'lisp-indent-function 1)
488a0b05 1002(put 'combine-after-change-calls 'lisp-indent-function 0)
38f16fe1 1003(put 'with-output-to-string 'lisp-indent-function 0)
08adb099 1004(put 'with-temp-file 'lisp-indent-function 1)
e0119683 1005(put 'with-temp-buffer 'lisp-indent-function 0)
bacd83a7 1006(put 'with-temp-message 'lisp-indent-function 1)
83c8f461 1007(put 'with-syntax-table 'lisp-indent-function 1)
a90256cc
BP
1008(put 'let 'lisp-indent-function 1)
1009(put 'let* 'lisp-indent-function 1)
1010(put 'while 'lisp-indent-function 1)
1011(put 'if 'lisp-indent-function 2)
0a88ae7b 1012(put 'read-if 'lisp-indent-function 2)
a90256cc
BP
1013(put 'catch 'lisp-indent-function 1)
1014(put 'condition-case 'lisp-indent-function 2)
1015(put 'unwind-protect 'lisp-indent-function 1)
1016(put 'with-output-to-temp-buffer 'lisp-indent-function 1)
b6b4c8bd 1017(put 'eval-after-load 'lisp-indent-function 1)
05c71036
DL
1018(put 'dolist 'lisp-indent-function 1)
1019(put 'dotimes 'lisp-indent-function 1)
1020(put 'when 'lisp-indent-function 1)
1021(put 'unless 'lisp-indent-function 1)
a90256cc
BP
1022
1023(defun indent-sexp (&optional endpos)
1024 "Indent each line of the list starting just after point.
1025If optional arg ENDPOS is given, indent each line, stopping when
1026ENDPOS is encountered."
1027 (interactive)
daa37602
JB
1028 (let ((indent-stack (list nil))
1029 (next-depth 0)
33f268ec
RS
1030 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1031 ;; so that calculate-lisp-indent will find the beginning of
1032 ;; the defun we are in.
1033 ;; If ENDPOS is nil, it is safe not to scan before point
1034 ;; since every line we indent is more deeply nested than point is.
1035 (starting-point (if endpos nil (point)))
daa37602
JB
1036 (last-point (point))
1037 last-depth bol outer-loop-done inner-loop-done state this-indent)
33f268ec
RS
1038 (or endpos
1039 ;; Get error now if we don't have a complete sexp after point.
1040 (save-excursion (forward-sexp 1)))
a90256cc
BP
1041 (save-excursion
1042 (setq outer-loop-done nil)
1043 (while (if endpos (< (point) endpos)
1044 (not outer-loop-done))
1045 (setq last-depth next-depth
1046 inner-loop-done nil)
1047 ;; Parse this line so we can learn the state
1048 ;; to indent the next line.
1049 ;; This inner loop goes through only once
1050 ;; unless a line ends inside a string.
1051 (while (and (not inner-loop-done)
1052 (not (setq outer-loop-done (eobp))))
1053 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1054 nil nil state))
1055 (setq next-depth (car state))
1056 ;; If the line contains a comment other than the sort
1057 ;; that is indented like code,
1058 ;; indent it now with indent-for-comment.
1059 ;; Comments indented like code are right already.
1060 ;; In any case clear the in-comment flag in the state
1061 ;; because parse-partial-sexp never sees the newlines.
1062 (if (car (nthcdr 4 state))
1063 (progn (indent-for-comment)
1064 (end-of-line)
1065 (setcar (nthcdr 4 state) nil)))
1066 ;; If this line ends inside a string,
1067 ;; go straight to next line, remaining within the inner loop,
1068 ;; and turn off the \-flag.
1069 (if (car (nthcdr 3 state))
1070 (progn
1071 (forward-line 1)
1072 (setcar (nthcdr 5 state) nil))
1073 (setq inner-loop-done t)))
1074 (and endpos
daa37602
JB
1075 (<= next-depth 0)
1076 (progn
99ec65b3
DL
1077 (setq indent-stack (nconc indent-stack
1078 (make-list (- next-depth) nil))
daa37602
JB
1079 last-depth (- last-depth next-depth)
1080 next-depth 0)))
33f268ec 1081 (or outer-loop-done endpos
a90256cc
BP
1082 (setq outer-loop-done (<= next-depth 0)))
1083 (if outer-loop-done
1f5038b5 1084 (forward-line 1)
a90256cc
BP
1085 (while (> last-depth next-depth)
1086 (setq indent-stack (cdr indent-stack)
1087 last-depth (1- last-depth)))
1088 (while (< last-depth next-depth)
1089 (setq indent-stack (cons nil indent-stack)
1090 last-depth (1+ last-depth)))
1091 ;; Now go to the next line and indent it according
1092 ;; to what we learned from parsing the previous one.
1093 (forward-line 1)
1094 (setq bol (point))
1095 (skip-chars-forward " \t")
1096 ;; But not if the line is blank, or just a comment
1097 ;; (except for double-semi comments; indent them as usual).
1098 (if (or (eobp) (looking-at "\\s<\\|\n"))
1099 nil
1100 (if (and (car indent-stack)
1101 (>= (car indent-stack) 0))
1102 (setq this-indent (car indent-stack))
1103 (let ((val (calculate-lisp-indent
1104 (if (car indent-stack) (- (car indent-stack))
daa37602 1105 starting-point))))
531cbff1
RS
1106 (if (null val)
1107 (setq this-indent val)
1108 (if (integerp val)
1109 (setcar indent-stack
1110 (setq this-indent val))
1111 (setcar indent-stack (- (car (cdr val))))
1112 (setq this-indent (car val))))))
1113 (if (and this-indent (/= (current-column) this-indent))
a90256cc
BP
1114 (progn (delete-region bol (point))
1115 (indent-to this-indent)))))
1116 (or outer-loop-done
1117 (setq outer-loop-done (= (point) last-point))
1118 (setq last-point (point)))))))
1119
a90256cc 1120(defun lisp-indent-region (start end)
535eadac 1121 "Indent every line whose first char is between START and END inclusive."
a90256cc 1122 (save-excursion
a90256cc 1123 (let ((endmark (copy-marker end)))
33f268ec
RS
1124 (goto-char start)
1125 (and (bolp) (not (eolp))
1126 (lisp-indent-line))
a90256cc
BP
1127 (indent-sexp endmark)
1128 (set-marker endmark nil))))
ca2ddd8e 1129
889bfc7d 1130(defun indent-pp-sexp (&optional arg)
5a77048a
RS
1131 "Indent each line of the list starting just after point, or prettyprint it.
1132A prefix argument specifies pretty-printing."
889bfc7d
JL
1133 (interactive "P")
1134 (if arg
1135 (save-excursion
1136 (save-restriction
1137 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1138 (pp-buffer)
1139 (goto-char (point-max))
1140 (if (eq (char-before) ?\n)
1141 (delete-char -1)))))
1142 (indent-sexp))
1143
6338c7ba
JB
1144;;;; Lisp paragraph filling commands.
1145
3e8737bf
MS
1146(defcustom emacs-lisp-docstring-fill-column 65
1147 "Value of `fill-column' to use when filling a docstring.
1148Any non-integer value means do not use a different value of
1149`fill-column' when filling docstrings."
1150 :type '(choice (integer)
1151 (const :tag "Use the current `fill-column'" t))
1152 :group 'lisp)
1153
6338c7ba 1154(defun lisp-fill-paragraph (&optional justify)
3e8737bf 1155 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
6338c7ba
JB
1156If any of the current line is a comment, fill the comment or the
1157paragraph of it that point is in, preserving the comment's indentation
1158and initial semicolons."
1159 (interactive "P")
833815e8 1160 (or (fill-comment-paragraph justify)
cd9d9561
SM
1161 ;; Since fill-comment-paragraph returned nil, that means we're not in
1162 ;; a comment: Point is on a program line; we are interested
3e8737bf
MS
1163 ;; particularly in docstring lines.
1164 ;;
1165 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1166 ;; are buffer-local, but we avoid changing them so that they can be set
1167 ;; to make `forward-paragraph' and friends do something the user wants.
1168 ;;
1169 ;; `paragraph-start': The `(' in the character alternative and the
1170 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1171 ;; sexps and backquoted sexps that follow a docstring from being filled
1172 ;; with the docstring. This setting has the consequence of inhibiting
1173 ;; filling many program lines that are not docstrings, which is sensible,
1174 ;; because the user probably asked to fill program lines by accident, or
1175 ;; expecting indentation (perhaps we should try to do indenting in that
1176 ;; case). The `;' and `:' stop the paragraph being filled at following
1177 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1178 ;; escaped to keep font-locking, filling, & paren matching in the source
1179 ;; file happy.
1180 ;;
1181 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1182 ;; a docstring and identifies it as a paragraph separator, so that it
1183 ;; won't be filled. (Since the first line of documentation stands alone
1184 ;; in some contexts, filling should not alter the contents the author has
1185 ;; chosen.) Only the first line of a docstring begins with whitespace
1186 ;; and a quotation mark and ends with a period or (rarely) a comma.
1187 ;;
1188 ;; The `fill-column' is temporarily bound to
1189 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
833815e8 1190 (let ((paragraph-start (concat paragraph-start
cd9d9561 1191 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
833815e8 1192 (paragraph-separate
3e8737bf
MS
1193 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1194 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1195 emacs-lisp-docstring-fill-column
1196 fill-column)))
833815e8
SM
1197 (fill-paragraph justify))
1198 ;; Never return nil.
1199 t))
ca2ddd8e 1200
a90256cc
BP
1201(defun indent-code-rigidly (start end arg &optional nochange-regexp)
1202 "Indent all lines of code, starting in the region, sideways by ARG columns.
1203Does not affect lines starting inside comments or strings, assuming that
1204the start of the region is not inside them.
1205
1206Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1207The last is a regexp which, if matched at the beginning of a line,
1208means don't indent that line."
1209 (interactive "r\np")
1210 (let (state)
1211 (save-excursion
1212 (goto-char end)
1213 (setq end (point-marker))
1214 (goto-char start)
1215 (or (bolp)
1216 (setq state (parse-partial-sexp (point)
1217 (progn
1218 (forward-line 1) (point))
1219 nil nil state)))
1220 (while (< (point) end)
1221 (or (car (nthcdr 3 state))
1222 (and nochange-regexp
1223 (looking-at nochange-regexp))
1224 ;; If line does not start in string, indent it
1225 (let ((indent (current-indentation)))
1226 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1227 (or (eolp)
1228 (indent-to (max 0 (+ indent arg)) 0))))
1229 (setq state (parse-partial-sexp (point)
1230 (progn
1231 (forward-line 1) (point))
1232 nil nil state))))))
49116ac0
JB
1233
1234(provide 'lisp-mode)
1235
cd9d9561 1236;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
6594deb0 1237;;; lisp-mode.el ends here