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