(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blobbb815481bf005b7f445d4e63d11967de6ed7de1f
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: lisp, languages
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
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.
26 ;;; Commentary:
28 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
29 ;; This mode is documented in the Emacs manual.
31 ;;; Code:
33 (defvar lisp-mode-abbrev-table nil)
35 (defvar emacs-lisp-mode-syntax-table
36 (let ((table (make-syntax-table)))
37 (let ((i 0))
38 (while (< i ?0)
39 (modify-syntax-entry i "_ " table)
40 (setq i (1+ i)))
41 (setq i (1+ ?9))
42 (while (< i ?A)
43 (modify-syntax-entry i "_ " table)
44 (setq i (1+ i)))
45 (setq i (1+ ?Z))
46 (while (< i ?a)
47 (modify-syntax-entry i "_ " table)
48 (setq i (1+ i)))
49 (setq i (1+ ?z))
50 (while (< i 128)
51 (modify-syntax-entry i "_ " table)
52 (setq i (1+ i)))
53 (modify-syntax-entry ? " " table)
54 (modify-syntax-entry ?\t " " table)
55 (modify-syntax-entry ?\f " " table)
56 (modify-syntax-entry ?\n "> " table)
57 ;; Give CR the same syntax as newline, for selective-display.
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)
63 (modify-syntax-entry ?@ "' " table)
64 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?\] ")[ " table))
73 table))
75 (defvar lisp-mode-syntax-table
76 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
77 (modify-syntax-entry ?\[ "_ " table)
78 (modify-syntax-entry ?\] "_ " table)
79 (modify-syntax-entry ?# "' 14bn" table)
80 (modify-syntax-entry ?| "\" 23b" table)
81 table))
83 (define-abbrev-table 'lisp-mode-abbrev-table ())
85 (defvar lisp-imenu-generic-expression
86 (list
87 (list nil
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"
94 "define-generic-mode"
95 "define-compiler-macro" "define-modify-macro"
96 "defsetf" "define-setf-expander"
97 "define-method-combination"
98 "defgeneric" "defmethod") t))
99 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
101 (list (purecopy "Variables")
102 (purecopy (concat "^\\s-*("
103 (eval-when-compile
104 (regexp-opt
105 '("defvar" "defconst" "defconstant" "defcustom"
106 "defparameter" "define-symbol-macro") t))
107 "\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
109 (list (purecopy "Types")
110 (purecopy (concat "^\\s-*("
111 (eval-when-compile
112 (regexp-opt
113 '("defgroup" "deftheme" "deftype" "defstruct"
114 "defclass" "define-condition" "define-widget"
115 "defface" "defpackage") t))
116 "\\s-+'?\\(\\sw\\(\\sw\\|\\s_\\)+\\)"))
119 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
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)
127 (put 'deftheme 'doc-string-elt 2)
128 (put 'defconst 'doc-string-elt 3)
129 (put 'defmacro 'doc-string-elt 3)
130 (put 'defmacro* 'doc-string-elt 3)
131 (put 'defsubst 'doc-string-elt 3)
132 (put 'define-skeleton 'doc-string-elt 2)
133 (put 'define-derived-mode 'doc-string-elt 4)
134 (put 'define-compilation-mode 'doc-string-elt 3)
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)
140 (put 'define-ibuffer-filter 'doc-string-elt 2)
141 (put 'define-ibuffer-op 'doc-string-elt 3)
142 (put 'define-ibuffer-sorter 'doc-string-elt 2)
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
152 (while (and (not (bobp))
153 (progn (backward-sexp 1) (setq n (1+ n)))))
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))
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)
167 (when lisp-syntax
168 (set-syntax-table lisp-mode-syntax-table))
169 (setq local-abbrev-table lisp-mode-abbrev-table)
170 (make-local-variable 'paragraph-ignore-fill-prefix)
171 (setq paragraph-ignore-fill-prefix t)
172 (make-local-variable 'fill-paragraph-function)
173 (setq fill-paragraph-function 'lisp-fill-paragraph)
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.
177 ;; I believe that newcomment's auto-fill code properly deals with it -stef
178 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
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)
185 (make-local-variable 'outline-regexp)
186 (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
187 (make-local-variable 'outline-level)
188 (setq outline-level 'lisp-outline-level)
189 (make-local-variable 'comment-start)
190 (setq comment-start ";")
191 (make-local-variable 'comment-start-skip)
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]\\)\\(\\\\\\\\\\)*\\);+ *")
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 ";+ *")
198 (make-local-variable 'comment-add)
199 (setq comment-add 1) ;default to `;;' in comment-region
200 (make-local-variable 'comment-column)
201 (setq comment-column 40)
202 ;; Don't get confused by `;' in doc strings when paragraph-filling.
203 (set (make-local-variable 'comment-use-global-state) t)
204 (make-local-variable 'imenu-generic-expression)
205 (setq imenu-generic-expression lisp-imenu-generic-expression)
206 (make-local-variable 'multibyte-syntax-as-symbol)
207 (setq multibyte-syntax-as-symbol t)
208 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
209 (setq font-lock-defaults
210 '((lisp-font-lock-keywords
211 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
212 nil nil (("+-*/.<>=!?$%_&~^:@" . "w")) nil
213 (font-lock-mark-block-function . mark-defun)
214 (font-lock-syntactic-face-function
215 . lisp-font-lock-syntactic-face-function))))
217 (defun lisp-outline-level ()
218 "Lisp mode `outline-level' function."
219 (let ((len (- (match-end 0) (match-beginning 0))))
220 (if (looking-at "(\\|;;;###autoload")
221 1000
222 len)))
224 (defvar lisp-mode-shared-map
225 (let ((map (make-sparse-keymap)))
226 (define-key map "\t" 'lisp-indent-line)
227 (define-key map "\e\C-q" 'indent-sexp)
228 (define-key map "\177" 'backward-delete-char-untabify)
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)
233 map)
234 "Keymap for commands shared by all sorts of Lisp modes.")
236 (defvar emacs-lisp-mode-map ()
237 "Keymap for Emacs Lisp mode.
238 All commands in `lisp-mode-shared-map' are inherited by this map.")
240 (if emacs-lisp-mode-map
242 (let ((map (make-sparse-keymap "Emacs-Lisp")))
243 (setq emacs-lisp-mode-map (make-sparse-keymap))
244 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map)
245 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
246 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun)
247 (define-key emacs-lisp-mode-map "\e\C-q" 'indent-pp-sexp)
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))
255 (define-key map [emacs-byte-compile-and-load]
256 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load))
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))
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)))
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)
276 (error "The buffer must be saved in a file first")))
278 (defun emacs-lisp-byte-compile-and-load ()
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.
285 (if (and (buffer-modified-p)
286 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
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))))
293 (defcustom emacs-lisp-mode-hook nil
294 "Hook run when entering Emacs Lisp mode."
295 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
296 :type 'hook
297 :group 'lisp)
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)
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)
311 (defun emacs-lisp-mode ()
312 "Major mode for editing Lisp code to run in Emacs.
313 Commands:
314 Delete converts tabs to spaces as it moves back.
315 Blank lines separate paragraphs. Semicolons start comments.
316 \\{emacs-lisp-mode-map}
317 Entry to this mode calls the value of `emacs-lisp-mode-hook'
318 if that value is non-nil."
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")
325 (lisp-mode-variables)
326 (setq imenu-case-fold-search nil)
327 (run-mode-hooks 'emacs-lisp-mode-hook))
328 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
330 (defvar lisp-mode-map
331 (let ((map (make-sparse-keymap)))
332 (set-keymap-parent map lisp-mode-shared-map)
333 (define-key map "\e\C-x" 'lisp-eval-defun)
334 (define-key map "\C-c\C-z" 'run-lisp)
335 map)
336 "Keymap for ordinary Lisp mode.
337 All commands in `lisp-mode-shared-map' are inherited by this map.")
339 (defun lisp-mode ()
340 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
341 Commands:
342 Delete converts tabs to spaces as it moves back.
343 Blank lines separate paragraphs. Semicolons start comments.
344 \\{lisp-mode-map}
345 Note that `run-lisp' may be used either to start an inferior Lisp job
346 or to switch back to an existing one.
348 Entry to this mode calls the value of `lisp-mode-hook'
349 if that value is non-nil."
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")
355 (lisp-mode-variables)
356 (make-local-variable 'comment-start-skip)
357 (setq comment-start-skip
358 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
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)
363 (run-mode-hooks 'lisp-mode-hook))
364 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
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))
371 default))))
373 ;; Used in old LispM code.
374 (defalias 'common-lisp-mode 'lisp-mode)
376 ;; This will do unless inf-lisp.el is loaded.
377 (defun lisp-eval-defun (&optional and-go)
378 "Send the current defun to the Lisp process made by \\[run-lisp]."
379 (interactive)
380 (error "Process lisp does not exist"))
382 (defvar lisp-interaction-mode-map
383 (let ((map (make-sparse-keymap)))
384 (set-keymap-parent map lisp-mode-shared-map)
385 (define-key map "\e\C-x" 'eval-defun)
386 (define-key map "\e\C-q" 'indent-pp-sexp)
387 (define-key map "\e\t" 'lisp-complete-symbol)
388 (define-key map "\n" 'eval-print-last-sexp)
389 map)
390 "Keymap for Lisp Interaction mode.
391 All commands in `lisp-mode-shared-map' are inherited by this map.")
393 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
394 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
395 "Major mode for typing and evaluating Lisp forms.
396 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
397 before point, and prints its value into the buffer, advancing point.
398 Note that printing is controlled by `eval-expression-print-length'
399 and `eval-expression-print-level'.
401 Commands:
402 Delete converts tabs to spaces as it moves back.
403 Paragraphs are separated only by blank lines.
404 Semicolons start comments.
405 \\{lisp-interaction-mode-map}
406 Entry to this mode calls the value of `lisp-interaction-mode-hook'
407 if that value is non-nil.")
409 (defun eval-print-last-sexp ()
410 "Evaluate sexp before point; print value into current buffer.
412 Note that printing the result is controlled by the variables
413 `eval-expression-print-length' and `eval-expression-print-level',
414 which see."
415 (interactive)
416 (let ((standard-output (current-buffer)))
417 (terpri)
418 (eval-last-sexp t)
419 (terpri)))
422 (defun last-sexp-setup-props (beg end value alt1 alt2)
423 "Set up text properties for the output of `eval-last-sexp-1'.
424 BEG and END are the start and end of the output in current-buffer.
425 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
426 alternative 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
432 beg end
433 `(printed-value (,value ,alt1 ,alt2)
434 mouse-face highlight
435 keymap ,map
436 help-echo "RET, mouse-2: toggle abbreviated display"
437 rear-nonsticky (mouse-face keymap help-echo
438 printed-value)))))
441 (defun last-sexp-toggle-display (&optional arg)
442 "Toggle between abbreviated and unabbreviated printed representations."
443 (interactive "P")
444 (save-restriction
445 (widen)
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)))))))
462 (defun prin1-char (char)
463 "Return a string representing CHAR as a character rather than as an integer.
464 If CHAR is not a character, return nil."
465 (and (integerp char)
466 (eventp char)
467 (let ((c (event-basic-type char))
468 (mods (event-modifiers char))
469 string)
470 ;; Prevent ?A from turning into ?\S-a.
471 (if (and (memq 'shift mods)
472 (zerop (logand char ?\S-\^@))
473 (not (let ((case-fold-search nil))
474 (char-equal c (upcase c)))))
475 (setq c (upcase c) mods nil))
476 ;; What string are we considering using?
477 (condition-case nil
478 (setq string
479 (concat
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-?")
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))))
499 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
500 "Evaluate sexp before point; print value in minibuffer.
501 With argument, print output into current buffer."
502 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
503 (let ((value
504 (eval (let ((stab (syntax-table))
505 (opoint (point))
506 ignore-quotes
507 expr)
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)))
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))))
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)))))))
554 (eval-last-sexp-print-value value))))
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)
561 (beg (point))
562 end)
563 (prog1
564 (prin1 value)
565 (let ((str (eval-expression-print-format value)))
566 (if str (princ str)))
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 ))))
579 (defvar eval-last-sexp-fake-value (make-symbol "t"))
581 (defun eval-last-sexp (eval-last-sexp-arg-internal)
582 "Evaluate sexp before point; print value in minibuffer.
583 Interactively, with prefix argument, print output into current buffer."
584 (interactive "P")
585 (if (null eval-expression-debug-on-error)
586 (eval-last-sexp-1 eval-last-sexp-arg-internal)
587 (let ((old-value eval-last-sexp-fake-value) new-value value)
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)))
595 (defun eval-defun-1 (form)
596 "Treat some expressions specially.
597 Reset the `defvar' and `defcustom' variables to the initial value.
598 Reinitialize the face according to the `defface' specification."
599 ;; The code in edebug-defun should be consistent with this, but not
600 ;; the same, since this gets a macroexpended form.
601 (cond ((not (listp form))
602 form)
603 ((and (eq (car form) 'defvar)
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))
608 (setq-default ,(nth 1 form) ,(nth 2 form))))
609 ;; `defcustom' is now macroexpanded to
610 ;; `custom-declare-variable' with a quoted value arg.
611 ((and (eq (car form) 'custom-declare-variable)
612 (default-boundp (eval (nth 1 form))))
613 ;; Force variable to be bound.
614 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
615 form)
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)
623 ((eq (car form) 'progn)
624 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
625 (t form)))
627 (defun eval-defun-2 ()
628 "Evaluate defun that point is in or before.
629 The value is displayed in the minibuffer.
630 If the current defun is actually a call to `defvar',
631 then reset the variable using the initial value expression
632 even if the variable already has some other value.
633 \(Normally `defvar' does not change the variable's value
634 if it already has a value.\)
636 With argument, insert value in current buffer after the defun.
637 Return the result of evaluation."
638 (interactive "P")
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
648 #'eval-region
649 (let ((standard-output t)
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)))
658 ;; Alter the form if necessary.
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))))))
666 ;; The result of evaluation has been put onto VALUES. So return it.
667 (car values))
669 (defun eval-defun (edebug-it)
670 "Evaluate the top-level form containing point, or after point.
672 If the current defun is actually a call to `defvar' or `defcustom',
673 evaluating it this way resets the variable using its initial value
674 expression even if the variable already has some other value.
675 \(Normally `defvar' and `defcustom' do not alter the value if there
676 already is one.)
678 With a prefix argument, instrument the code for Edebug.
680 If acting on a `defun' for FUNCTION, and the function was
681 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
682 instrumented, just FUNCTION is printed.
684 If not acting on a `defun', the result of evaluation is displayed in
685 the minibuffer. This display is controlled by the variables
686 `eval-expression-print-length' and `eval-expression-print-level',
687 which see."
688 (interactive "P")
689 (cond (edebug-it
690 (require 'edebug)
691 (eval-defun (not edebug-all-defs)))
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)))))
704 (defun lisp-comment-indent ()
705 (if (looking-at "\\s<\\s<\\s<")
706 (current-column)
707 (if (looking-at "\\s<\\s<")
708 (let ((tem (or (calculate-lisp-indent) (current-column))))
709 (if (listp tem) (car tem) tem))
710 (skip-chars-backward " \t")
711 (max (if (bolp) 0 (1+ (current-column)))
712 comment-column))))
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.
717 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
718 ;; so we could get rid of it. -stef
719 (defun lisp-mode-auto-fill ()
720 (if (> (current-column) (current-fill-column))
721 (if (save-excursion
722 (nth 4 (syntax-ppss (point))))
723 (do-auto-fill)
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))))))
729 (defvar lisp-indent-offset nil
730 "If non-nil, indent second line of expressions that many more columns.")
731 (defvar lisp-indent-function 'lisp-indent-function)
733 (defun lisp-indent-line (&optional whole-exp)
734 "Indent current line as Lisp code.
735 With argument, indent any additional lines of the same expression
736 rigidly along with this one."
737 (interactive "P")
738 (let ((indent (calculate-lisp-indent)) shift-amt end
739 (pos (- (point-max) (point)))
740 (beg (progn (beginning-of-line) (point))))
741 (skip-chars-forward " \t")
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.
745 (goto-char (- (point-max) pos))
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)
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)))))
772 (defvar calculate-lisp-indent-last-sexp)
774 (defun calculate-lisp-indent (&optional parse-start)
775 "Return appropriate indentation for current line as Lisp code.
776 In usual case returns an integer: the column to indent to.
777 If the value is nil, that means don't change the indentation
778 because the line starts inside a string.
780 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
781 This means that following lines at the same level of indentation
782 should not necessarily be indented the same as this line.
783 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
784 is the buffer position of the start of the containing expression."
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)
792 calculate-lisp-indent-last-sexp containing-sexp)
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)
804 (setq calculate-lisp-indent-last-sexp (elt state 2))
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?
809 (if (and calculate-lisp-indent-last-sexp
810 (> calculate-lisp-indent-last-sexp (point)))
811 ;; Yes, but is there a containing sexp after that?
812 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
813 indent-point 0)))
814 (if (setq retry (car (cdr peek))) (setq state peek)))))
815 (if retry
817 ;; Innermost containing sexp found
818 (goto-char (1+ containing-sexp))
819 (if (not calculate-lisp-indent-last-sexp)
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.
824 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
825 (cond ((looking-at "\\s(")
826 ;; First element of containing sexp is a list.
827 ;; Indent under that list.
829 ((> (save-excursion (forward-line 1) (point))
830 calculate-lisp-indent-last-sexp)
831 ;; This is the first line to start within the containing sexp.
832 ;; It's almost certainly a function call.
833 (if (= (point) calculate-lisp-indent-last-sexp)
834 ;; Containing sexp has nothing before this line
835 ;; except the first element. Indent under that element.
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)
840 (parse-partial-sexp (point)
841 calculate-lisp-indent-last-sexp
842 0 t)))
843 (backward-prefix-chars))
845 ;; Indent beneath first sexp on same line as
846 ;; `calculate-lisp-indent-last-sexp'. Again, it's
847 ;; almost certainly a function call.
848 (goto-char calculate-lisp-indent-last-sexp)
849 (beginning-of-line)
850 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
851 0 t)
852 (backward-prefix-chars)))))
853 ;; Point is at the point to indent under unless we are inside a string.
854 ;; Call indentation hook except when overridden by lisp-indent-offset
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.
859 nil)
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))
871 normal-indent))))))
873 (defun lisp-indent-function (indent-point state)
874 "This function is the normal value of the variable `lisp-indent-function'.
875 It is used when indenting a line within a function call, to see if the
876 called function says anything special about how to indent the line.
878 INDENT-POINT is the position where the user typed TAB, or equivalent.
879 Point is located at the point to indent under (for default indentation);
880 STATE is the `parse-partial-sexp' state for that position.
882 If the current line is in a call to a Lisp function
883 which has a non-nil property `lisp-indent-function',
884 that 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
887 like ordinary function arguments and then indent any further
888 arguments like a body;
889 * a function to call just as this function was called.
890 If that function returns nil, that means it doesn't specify
891 the indentation.
893 This function also returns nil meaning don't specify the indentation."
894 (let ((normal-indent (current-column)))
895 (goto-char (1+ (elt state 1)))
896 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
897 (if (and (elt state 2)
898 (not (looking-at "\\sw\\|\\s_")))
899 ;; car of form doesn't seem to be a symbol
900 (progn
901 (if (not (> (save-excursion (forward-line 1) (point))
902 calculate-lisp-indent-last-sexp))
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
910 ;; inside the innermost containing sexp.
911 (backward-prefix-chars)
912 (current-column))
913 (let ((function (buffer-substring (point)
914 (progn (forward-sexp 1) (point))))
915 method)
916 (setq method (or (get (intern-soft function) 'lisp-indent-function)
917 (get (intern-soft function) 'lisp-indent-hook)))
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
927 (funcall method indent-point state)))))))
929 (defvar lisp-body-indent 2
930 "Number of columns to indent the second line of a `(def...)' form.")
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))))
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)))))
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).
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)
997 (put 'save-selected-window 'lisp-indent-function 0)
998 (put 'save-restriction 'lisp-indent-function 0)
999 (put 'save-match-data 'lisp-indent-function 0)
1000 (put 'save-current-buffer 'lisp-indent-function 0)
1001 (put 'with-current-buffer 'lisp-indent-function 1)
1002 (put 'combine-after-change-calls 'lisp-indent-function 0)
1003 (put 'with-output-to-string 'lisp-indent-function 0)
1004 (put 'with-temp-file 'lisp-indent-function 1)
1005 (put 'with-temp-buffer 'lisp-indent-function 0)
1006 (put 'with-temp-message 'lisp-indent-function 1)
1007 (put 'with-syntax-table 'lisp-indent-function 1)
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)
1012 (put 'read-if 'lisp-indent-function 2)
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)
1017 (put 'eval-after-load 'lisp-indent-function 1)
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)
1023 (defun indent-sexp (&optional endpos)
1024 "Indent each line of the list starting just after point.
1025 If optional arg ENDPOS is given, indent each line, stopping when
1026 ENDPOS is encountered."
1027 (interactive)
1028 (let ((indent-stack (list nil))
1029 (next-depth 0)
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)))
1036 (last-point (point))
1037 last-depth bol outer-loop-done inner-loop-done state this-indent)
1038 (or endpos
1039 ;; Get error now if we don't have a complete sexp after point.
1040 (save-excursion (forward-sexp 1)))
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
1075 (<= next-depth 0)
1076 (progn
1077 (setq indent-stack (nconc indent-stack
1078 (make-list (- next-depth) nil))
1079 last-depth (- last-depth next-depth)
1080 next-depth 0)))
1081 (or outer-loop-done endpos
1082 (setq outer-loop-done (<= next-depth 0)))
1083 (if outer-loop-done
1084 (forward-line 1)
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"))
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))
1105 starting-point))))
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))
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)))))))
1120 (defun lisp-indent-region (start end)
1121 "Indent every line whose first char is between START and END inclusive."
1122 (save-excursion
1123 (let ((endmark (copy-marker end)))
1124 (goto-char start)
1125 (and (bolp) (not (eolp))
1126 (lisp-indent-line))
1127 (indent-sexp endmark)
1128 (set-marker endmark nil))))
1130 (defun indent-pp-sexp (&optional arg)
1131 "Indent each line of the list starting just after point, or prettyprint it.
1132 A prefix argument specifies pretty-printing."
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))
1144 ;;;; Lisp paragraph filling commands.
1146 (defcustom emacs-lisp-docstring-fill-column 65
1147 "Value of `fill-column' to use when filling a docstring.
1148 Any 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)
1154 (defun lisp-fill-paragraph (&optional justify)
1155 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1156 If any of the current line is a comment, fill the comment or the
1157 paragraph of it that point is in, preserving the comment's indentation
1158 and initial semicolons."
1159 (interactive "P")
1160 (or (fill-comment-paragraph justify)
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
1163 ;; particularly in docstring lines.
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.
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.
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.
1188 ;; The `fill-column' is temporarily bound to
1189 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1190 (let ((paragraph-start (concat paragraph-start
1191 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1192 (paragraph-separate
1193 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1194 (fill-column (if (integerp emacs-lisp-docstring-fill-column)
1195 emacs-lisp-docstring-fill-column
1196 fill-column)))
1197 (fill-paragraph justify))
1198 ;; Never return nil.
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.
1203 Does not affect lines starting inside comments or strings, assuming that
1204 the start of the region is not inside them.
1206 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1207 The last is a regexp which, if matched at the beginning of a line,
1208 means 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))))))
1234 (provide 'lisp-mode)
1236 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1237 ;;; lisp-mode.el ends here