1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
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 3, or (at your option)
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
29 ;; This mode is documented in the Emacs manual.
33 (defvar font-lock-comment-face
)
34 (defvar font-lock-doc-face
)
35 (defvar font-lock-keywords-case-fold-search
)
36 (defvar font-lock-string-face
)
38 (defvar lisp-mode-abbrev-table nil
)
40 (defvar emacs-lisp-mode-syntax-table
41 (let ((table (make-syntax-table)))
44 (modify-syntax-entry i
"_ " table
)
48 (modify-syntax-entry i
"_ " table
)
52 (modify-syntax-entry i
"_ " table
)
56 (modify-syntax-entry i
"_ " table
)
58 (modify-syntax-entry ?\s
" " table
)
59 ;; Non-break space acts as whitespace.
60 (modify-syntax-entry ?
\x8a0 " " table
)
61 (modify-syntax-entry ?
\t " " table
)
62 (modify-syntax-entry ?
\f " " table
)
63 (modify-syntax-entry ?
\n "> " table
)
64 ;; This is probably obsolete since nowadays such features use overlays.
65 ;; ;; Give CR the same syntax as newline, for selective-display.
66 ;; (modify-syntax-entry ?\^m "> " 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 ;; Used to be singlequote; changed for flonums.
73 (modify-syntax-entry ?.
"_ " table
)
74 (modify-syntax-entry ?
# "' " table
)
75 (modify-syntax-entry ?
\" "\" " table
)
76 (modify-syntax-entry ?
\\ "\\ " table
)
77 (modify-syntax-entry ?\
( "() " table
)
78 (modify-syntax-entry ?\
) ")( " table
)
79 (modify-syntax-entry ?\
[ "(] " table
)
80 (modify-syntax-entry ?\
] ")[ " table
))
83 (defvar lisp-mode-syntax-table
84 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
85 (modify-syntax-entry ?\
[ "_ " table
)
86 (modify-syntax-entry ?\
] "_ " table
)
87 (modify-syntax-entry ?
# "' 14b" table
)
88 (modify-syntax-entry ?|
"\" 23bn" table
)
91 (define-abbrev-table 'lisp-mode-abbrev-table
())
93 (defvar lisp-imenu-generic-expression
96 (purecopy (concat "^\\s-*("
99 '("defun" "defun*" "defsubst" "defmacro"
100 "defadvice" "define-skeleton"
101 "define-minor-mode" "define-global-minor-mode"
102 "define-globalized-minor-mode"
103 "define-derived-mode" "define-generic-mode"
104 "define-compiler-macro" "define-modify-macro"
105 "defsetf" "define-setf-expander"
106 "define-method-combination"
107 "defgeneric" "defmethod") t
))
108 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
110 (list (purecopy "Variables")
111 (purecopy (concat "^\\s-*("
114 '("defvar" "defconst" "defconstant" "defcustom"
115 "defparameter" "define-symbol-macro") t
))
116 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
118 (list (purecopy "Types")
119 (purecopy (concat "^\\s-*("
122 '("defgroup" "deftheme" "deftype" "defstruct"
123 "defclass" "define-condition" "define-widget"
124 "defface" "defpackage") t
))
125 "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
128 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
130 ;; This was originally in autoload.el and is still used there.
131 (put 'autoload
'doc-string-elt
3)
132 (put 'defun
'doc-string-elt
3)
133 (put 'defun
* 'doc-string-elt
3)
134 (put 'defvar
'doc-string-elt
3)
135 (put 'defcustom
'doc-string-elt
3)
136 (put 'deftheme
'doc-string-elt
2)
137 (put 'defconst
'doc-string-elt
3)
138 (put 'defmacro
'doc-string-elt
3)
139 (put 'defmacro
* 'doc-string-elt
3)
140 (put 'defsubst
'doc-string-elt
3)
141 (put 'defstruct
'doc-string-elt
2)
142 (put 'define-skeleton
'doc-string-elt
2)
143 (put 'define-derived-mode
'doc-string-elt
4)
144 (put 'define-compilation-mode
'doc-string-elt
3)
145 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
146 (put 'define-minor-mode
'doc-string-elt
2)
147 (put 'easy-mmode-define-global-mode
'doc-string-elt
2)
148 (put 'define-global-minor-mode
'doc-string-elt
2)
149 (put 'define-globalized-minor-mode
'doc-string-elt
2)
150 (put 'define-generic-mode
'doc-string-elt
7)
151 (put 'define-ibuffer-filter
'doc-string-elt
2)
152 (put 'define-ibuffer-op
'doc-string-elt
3)
153 (put 'define-ibuffer-sorter
'doc-string-elt
2)
154 (put 'lambda
'doc-string-elt
2)
155 (put 'defalias
'doc-string-elt
3)
156 (put 'defvaralias
'doc-string-elt
3)
157 (put 'define-category
'doc-string-elt
2)
159 (defvar lisp-doc-string-elt-property
'doc-string-elt
160 "The symbol property that holds the docstring position info.")
162 (defun lisp-font-lock-syntactic-face-function (state)
164 ;; This might be a (doc)string or a |...| symbol.
165 (let ((startpos (nth 8 state
)))
166 (if (eq (char-after startpos
) ?|
)
167 ;; This is not a string, but a |...| symbol.
169 (let* ((listbeg (nth 1 state
))
170 (firstsym (and listbeg
173 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
175 (docelt (and firstsym
(get (intern-soft firstsym
)
176 lisp-doc-string-elt-property
))))
178 ;; It's a string in a form that can have a docstring.
179 ;; Check whether it's in docstring position.
181 (when (functionp docelt
)
182 (goto-char (match-end 1))
183 (setq docelt
(funcall docelt
)))
187 (while (and (> docelt
0) (< (point) startpos
)
188 (progn (forward-sexp 1) t
))
189 (setq docelt
(1- docelt
)))
191 (and (zerop docelt
) (<= (point) startpos
)
192 (progn (forward-comment (point-max)) t
)
193 (= (point) (nth 8 state
)))))
195 font-lock-string-face
))))
196 font-lock-comment-face
))
198 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
199 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
200 (defun lisp-mode-variables (&optional lisp-syntax
)
202 (set-syntax-table lisp-mode-syntax-table
))
203 (setq local-abbrev-table lisp-mode-abbrev-table
)
204 (make-local-variable 'paragraph-ignore-fill-prefix
)
205 (setq paragraph-ignore-fill-prefix t
)
206 (make-local-variable 'fill-paragraph-function
)
207 (setq fill-paragraph-function
'lisp-fill-paragraph
)
208 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
209 ;; a single docstring. Let's fix it here.
210 (set (make-local-variable 'adaptive-fill-function
)
211 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
212 ;; Adaptive fill mode gets in the way of auto-fill,
213 ;; and should make no difference for explicit fill
214 ;; because lisp-fill-paragraph should do the job.
215 ;; I believe that newcomment's auto-fill code properly deals with it -stef
216 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
217 (make-local-variable 'indent-line-function
)
218 (setq indent-line-function
'lisp-indent-line
)
219 (make-local-variable 'indent-region-function
)
220 (setq indent-region-function
'lisp-indent-region
)
221 (make-local-variable 'parse-sexp-ignore-comments
)
222 (setq parse-sexp-ignore-comments t
)
223 (make-local-variable 'outline-regexp
)
224 (setq outline-regexp
";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
225 (make-local-variable 'outline-level
)
226 (setq outline-level
'lisp-outline-level
)
227 (make-local-variable 'comment-start
)
228 (setq comment-start
";")
229 (make-local-variable 'comment-start-skip
)
230 ;; Look within the line for a ; following an even number of backslashes
231 ;; after either a non-backslash or the line beginning.
232 (setq comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
233 (make-local-variable 'font-lock-comment-start-skip
)
234 ;; Font lock mode uses this only when it KNOWS a comment is starting.
235 (setq font-lock-comment-start-skip
";+ *")
236 (make-local-variable 'comment-add
)
237 (setq comment-add
1) ;default to `;;' in comment-region
238 (make-local-variable 'comment-column
)
239 (setq comment-column
40)
240 ;; Don't get confused by `;' in doc strings when paragraph-filling.
241 (set (make-local-variable 'comment-use-global-state
) t
)
242 (make-local-variable 'imenu-generic-expression
)
243 (setq imenu-generic-expression lisp-imenu-generic-expression
)
244 (make-local-variable 'multibyte-syntax-as-symbol
)
245 (setq multibyte-syntax-as-symbol t
)
246 (set (make-local-variable 'syntax-begin-function
) 'beginning-of-defun
)
247 (setq font-lock-defaults
248 '((lisp-font-lock-keywords
249 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
250 nil nil
(("+-*/.<>=!?$%_&~^:@" .
"w")) nil
251 (font-lock-mark-block-function . mark-defun
)
252 (font-lock-syntactic-face-function
253 . lisp-font-lock-syntactic-face-function
))))
255 (defun lisp-outline-level ()
256 "Lisp mode `outline-level' function."
257 (let ((len (- (match-end 0) (match-beginning 0))))
258 (if (looking-at "(\\|;;;###autoload")
262 (defvar lisp-mode-shared-map
263 (let ((map (make-sparse-keymap)))
264 (define-key map
"\t" 'lisp-indent-line
)
265 (define-key map
"\e\C-q" 'indent-sexp
)
266 (define-key map
"\177" 'backward-delete-char-untabify
)
267 ;; This gets in the way when viewing a Lisp file in view-mode. As
268 ;; long as [backspace] is mapped into DEL via the
269 ;; function-key-map, this should remain disabled!!
270 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
272 "Keymap for commands shared by all sorts of Lisp modes.")
274 (defvar emacs-lisp-mode-map
()
275 "Keymap for Emacs Lisp mode.
276 All commands in `lisp-mode-shared-map' are inherited by this map.")
278 (if emacs-lisp-mode-map
280 (let ((map (make-sparse-keymap "Emacs-Lisp")))
281 (setq emacs-lisp-mode-map
(make-sparse-keymap))
282 (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map
)
283 (define-key emacs-lisp-mode-map
"\e\t" 'lisp-complete-symbol
)
284 (define-key emacs-lisp-mode-map
"\e\C-x" 'eval-defun
)
285 (define-key emacs-lisp-mode-map
"\e\C-q" 'indent-pp-sexp
)
286 (define-key emacs-lisp-mode-map
[menu-bar
] (make-sparse-keymap))
287 (define-key emacs-lisp-mode-map
[menu-bar emacs-lisp
]
288 (cons "Emacs-Lisp" map
))
289 (define-key map
[edebug-defun
]
290 '("Instrument Function for Debugging" . edebug-defun
))
291 (define-key map
[byte-recompile
]
292 '("Byte-recompile Directory..." . byte-recompile-directory
))
293 (define-key map
[emacs-byte-compile-and-load
]
294 '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load
))
295 (define-key map
[byte-compile
]
296 '("Byte-compile This File" . emacs-lisp-byte-compile
))
297 (define-key map
[separator-eval
] '("--"))
298 (define-key map
[eval-buffer
] '("Evaluate Buffer" . eval-buffer
))
299 (define-key map
[eval-region
] '("Evaluate Region" . eval-region
))
300 (define-key map
[eval-sexp
] '("Evaluate Last S-expression" . eval-last-sexp
))
301 (define-key map
[separator-format
] '("--"))
302 (define-key map
[comment-region
] '("Comment Out Region" . comment-region
))
303 (define-key map
[indent-region
] '("Indent Region" . indent-region
))
304 (define-key map
[indent-line
] '("Indent Line" . lisp-indent-line
))
305 (put 'eval-region
'menu-enable
'mark-active
)
306 (put 'comment-region
'menu-enable
'mark-active
)
307 (put 'indent-region
'menu-enable
'mark-active
)))
309 (defun emacs-lisp-byte-compile ()
310 "Byte compile the file containing the current buffer."
313 (byte-compile-file buffer-file-name
)
314 (error "The buffer must be saved in a file first")))
316 (defun emacs-lisp-byte-compile-and-load ()
317 "Byte-compile the current file (if it has changed), then load compiled code."
320 (error "The buffer must be saved in a file first"))
322 ;; Recompile if file or buffer has changed since last compilation.
323 (if (and (buffer-modified-p)
324 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
326 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name
)))
327 (if (file-newer-than-file-p compiled-file-name buffer-file-name
)
328 (load-file compiled-file-name
)
329 (byte-compile-file buffer-file-name t
))))
331 (defcustom emacs-lisp-mode-hook nil
332 "Hook run when entering Emacs Lisp mode."
333 :options
'(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode
)
337 (defcustom lisp-mode-hook nil
338 "Hook run when entering Lisp mode."
339 :options
'(imenu-add-menubar-index)
343 (defcustom lisp-interaction-mode-hook nil
344 "Hook run when entering Lisp Interaction mode."
345 :options
'(turn-on-eldoc-mode)
349 (defun emacs-lisp-mode ()
350 "Major mode for editing Lisp code to run in Emacs.
352 Delete converts tabs to spaces as it moves back.
353 Blank lines separate paragraphs. Semicolons start comments.
354 \\{emacs-lisp-mode-map}
355 Entry to this mode calls the value of `emacs-lisp-mode-hook'
356 if that value is non-nil."
358 (kill-all-local-variables)
359 (use-local-map emacs-lisp-mode-map
)
360 (set-syntax-table emacs-lisp-mode-syntax-table
)
361 (setq major-mode
'emacs-lisp-mode
)
362 (setq mode-name
"Emacs-Lisp")
363 (lisp-mode-variables)
364 (setq imenu-case-fold-search nil
)
365 (run-mode-hooks 'emacs-lisp-mode-hook
))
366 (put 'emacs-lisp-mode
'custom-mode-group
'lisp
)
368 (defvar lisp-mode-map
369 (let ((map (make-sparse-keymap)))
370 (set-keymap-parent map lisp-mode-shared-map
)
371 (define-key map
"\e\C-x" 'lisp-eval-defun
)
372 (define-key map
"\C-c\C-z" 'run-lisp
)
374 "Keymap for ordinary Lisp mode.
375 All commands in `lisp-mode-shared-map' are inherited by this map.")
378 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
380 Delete converts tabs to spaces as it moves back.
381 Blank lines separate paragraphs. Semicolons start comments.
383 Note that `run-lisp' may be used either to start an inferior Lisp job
384 or to switch back to an existing one.
386 Entry to this mode calls the value of `lisp-mode-hook'
387 if that value is non-nil."
389 (kill-all-local-variables)
390 (use-local-map lisp-mode-map
)
391 (setq major-mode
'lisp-mode
)
392 (setq mode-name
"Lisp")
393 (lisp-mode-variables)
394 (make-local-variable 'comment-start-skip
)
395 (setq comment-start-skip
396 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
397 (make-local-variable 'font-lock-keywords-case-fold-search
)
398 (setq font-lock-keywords-case-fold-search t
)
399 (setq imenu-case-fold-search t
)
400 (set-syntax-table lisp-mode-syntax-table
)
401 (run-mode-hooks 'lisp-mode-hook
))
402 (put 'lisp-mode
'find-tag-default-function
'lisp-find-tag-default
)
404 (defun lisp-find-tag-default ()
405 (let ((default (find-tag-default)))
406 (when (stringp default
)
407 (if (string-match ":+" default
)
408 (substring default
(match-end 0))
411 ;; Used in old LispM code.
412 (defalias 'common-lisp-mode
'lisp-mode
)
414 ;; This will do unless inf-lisp.el is loaded.
415 (defun lisp-eval-defun (&optional and-go
)
416 "Send the current defun to the Lisp process made by \\[run-lisp]."
418 (error "Process lisp does not exist"))
420 (defvar lisp-interaction-mode-map
421 (let ((map (make-sparse-keymap)))
422 (set-keymap-parent map lisp-mode-shared-map
)
423 (define-key map
"\e\C-x" 'eval-defun
)
424 (define-key map
"\e\C-q" 'indent-pp-sexp
)
425 (define-key map
"\e\t" 'lisp-complete-symbol
)
426 (define-key map
"\n" 'eval-print-last-sexp
)
428 "Keymap for Lisp Interaction mode.
429 All commands in `lisp-mode-shared-map' are inherited by this map.")
431 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table
)
432 (define-derived-mode lisp-interaction-mode emacs-lisp-mode
"Lisp Interaction"
433 "Major mode for typing and evaluating Lisp forms.
434 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
435 before point, and prints its value into the buffer, advancing point.
436 Note that printing is controlled by `eval-expression-print-length'
437 and `eval-expression-print-level'.
440 Delete converts tabs to spaces as it moves back.
441 Paragraphs are separated only by blank lines.
442 Semicolons start comments.
443 \\{lisp-interaction-mode-map}
444 Entry to this mode calls the value of `lisp-interaction-mode-hook'
445 if that value is non-nil.")
447 (defun eval-print-last-sexp ()
448 "Evaluate sexp before point; print value into current buffer.
450 If `eval-expression-debug-on-error' is non-nil, which is the default,
451 this command arranges for all errors to enter the debugger.
453 Note that printing the result is controlled by the variables
454 `eval-expression-print-length' and `eval-expression-print-level',
457 (let ((standard-output (current-buffer)))
463 (defun last-sexp-setup-props (beg end value alt1 alt2
)
464 "Set up text properties for the output of `eval-last-sexp-1'.
465 BEG and END are the start and end of the output in current-buffer.
466 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
467 alternative printed representations that can be displayed."
468 (let ((map (make-sparse-keymap)))
469 (define-key map
"\C-m" 'last-sexp-toggle-display
)
470 (define-key map
[down-mouse-2
] 'mouse-set-point
)
471 (define-key map
[mouse-2
] 'last-sexp-toggle-display
)
474 `(printed-value (,value
,alt1
,alt2
)
477 help-echo
"RET, mouse-2: toggle abbreviated display"
478 rear-nonsticky
(mouse-face keymap help-echo
482 (defun last-sexp-toggle-display (&optional arg
)
483 "Toggle between abbreviated and unabbreviated printed representations."
487 (let ((value (get-text-property (point) 'printed-value
)))
489 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
492 (end (or (next-single-char-property-change (point) 'printed-value
) (point)))
493 (standard-output (current-buffer))
495 (delete-region beg end
)
496 (insert (nth 1 value
))
498 (setq point
(1- (point))))
499 (last-sexp-setup-props beg
(point)
503 (goto-char (min (point-max) point
)))))))
505 (defun prin1-char (char)
506 "Return a string representing CHAR as a character rather than as an integer.
507 If CHAR is not a character, return nil."
510 (let ((c (event-basic-type char
))
511 (mods (event-modifiers char
))
513 ;; Prevent ?A from turning into ?\S-a.
514 (if (and (memq 'shift mods
)
515 (zerop (logand char ?\S-\^
@))
516 (not (let ((case-fold-search nil
))
517 (char-equal c
(upcase c
)))))
518 (setq c
(upcase c
) mods nil
))
519 ;; What string are we considering using?
526 (cond ((eq modif
'super
) "\\s-")
527 (t (string ?
\\ (upcase (aref (symbol-name modif
) 0)) ?-
))))
530 ((memq c
'(?\
; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
535 ;; Verify the string reads a CHAR, not to some other character.
536 ;; If it doesn't, return nil instead.
538 (= (car (read-from-string string
)) char
)
542 (defun preceding-sexp ()
543 "Return sexp before the point."
544 (let ((opoint (point))
548 (with-syntax-table emacs-lisp-mode-syntax-table
549 ;; If this sexp appears to be enclosed in `...'
550 ;; then ignore the surrounding quotes.
552 (or (eq (following-char) ?
\')
553 (eq (preceding-char) ?
\')))
555 ;; If we were after `?\e' (or similar case),
556 ;; use the whole thing, not just the `e'.
557 (when (eq (preceding-char) ?
\\)
559 (when (eq (preceding-char) ??
)
563 (when (eq (preceding-char) ?
=)
566 (skip-chars-backward "0-9#=")
567 (setq labeled-p
(looking-at "\\(#[0-9]+=\\)+")))
572 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
573 ;; `variable' so that the value is returned, not the
575 (if (and ignore-quotes
576 (eq (following-char) ?
`))
578 (narrow-to-region (point-min) opoint
)
579 (setq expr
(read (current-buffer)))
580 ;; If it's an (interactive ...) form, it's more
581 ;; useful to show how an interactive call would
584 (eq (car expr
) 'interactive
)
586 (list 'call-interactively
595 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
596 "Evaluate sexp before point; print value in minibuffer.
597 With argument, print output into current buffer."
598 (let ((standard-output (if eval-last-sexp-arg-internal
(current-buffer) t
)))
599 (eval-last-sexp-print-value (eval (preceding-sexp)))))
602 (defun eval-last-sexp-print-value (value)
603 (let ((unabbreviated (let ((print-length nil
) (print-level nil
))
604 (prin1-to-string value
)))
605 (print-length eval-expression-print-length
)
606 (print-level eval-expression-print-level
)
611 (let ((str (eval-expression-print-format value
)))
612 (if str
(princ str
)))
614 (when (and (bufferp standard-output
)
615 (or (not (null print-length
))
616 (not (null print-level
)))
617 (not (string= unabbreviated
618 (buffer-substring-no-properties beg end
))))
619 (last-sexp-setup-props beg end value
621 (buffer-substring-no-properties beg end
))
625 (defvar eval-last-sexp-fake-value
(make-symbol "t"))
627 (defun eval-last-sexp (eval-last-sexp-arg-internal)
628 "Evaluate sexp before point; print value in minibuffer.
629 Interactively, with prefix argument, print output into current buffer.
631 If `eval-expression-debug-on-error' is non-nil, which is the default,
632 this command arranges for all errors to enter the debugger."
634 (if (null eval-expression-debug-on-error
)
635 (eval-last-sexp-1 eval-last-sexp-arg-internal
)
637 (let ((debug-on-error eval-last-sexp-fake-value
))
638 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal
)
640 (unless (eq (cdr value
) eval-last-sexp-fake-value
)
641 (setq debug-on-error
(cdr value
)))
644 (defun eval-defun-1 (form)
645 "Treat some expressions specially.
646 Reset the `defvar' and `defcustom' variables to the initial value.
647 Reinitialize the face according to the `defface' specification."
648 ;; The code in edebug-defun should be consistent with this, but not
649 ;; the same, since this gets a macroexpended form.
650 (cond ((not (listp form
))
652 ((and (eq (car form
) 'defvar
)
653 (cdr-safe (cdr-safe form
))
654 (boundp (cadr form
)))
655 ;; Force variable to be re-set.
656 `(progn (defvar ,(nth 1 form
) nil
,@(nthcdr 3 form
))
657 (setq-default ,(nth 1 form
) ,(nth 2 form
))))
658 ;; `defcustom' is now macroexpanded to
659 ;; `custom-declare-variable' with a quoted value arg.
660 ((and (eq (car form
) 'custom-declare-variable
)
661 (default-boundp (eval (nth 1 form
))))
662 ;; Force variable to be bound.
663 (set-default (eval (nth 1 form
)) (eval (nth 1 (nth 2 form
))))
665 ;; `defface' is macroexpanded to `custom-declare-face'.
666 ((eq (car form
) 'custom-declare-face
)
668 (setq face-new-frame-defaults
669 (assq-delete-all (eval (nth 1 form
)) face-new-frame-defaults
))
670 (put (eval (nth 1 form
)) 'face-defface-spec nil
)
671 ;; Setting `customized-face' to the new spec after calling
672 ;; the form, but preserving the old saved spec in `saved-face',
673 ;; imitates the situation when the new face spec is set
674 ;; temporarily for the current session in the customize
675 ;; buffer, thus allowing `face-user-default-spec' to use the
676 ;; new customized spec instead of the saved spec.
677 ;; Resetting `saved-face' temporarily to nil is needed to let
678 ;; `defface' change the spec, regardless of a saved spec.
680 (put ,(nth 1 form
) 'saved-face
681 ',(get (eval (nth 1 form
)) 'saved-face
))
682 (put ,(nth 1 form
) 'customized-face
684 (put (eval (nth 1 form
)) 'saved-face nil
)))
685 ((eq (car form
) 'progn
)
686 (cons 'progn
(mapcar 'eval-defun-1
(cdr form
))))
689 (defun eval-defun-2 ()
690 "Evaluate defun that point is in or before.
691 The value is displayed in the minibuffer.
692 If the current defun is actually a call to `defvar',
693 then reset the variable using the initial value expression
694 even if the variable already has some other value.
695 \(Normally `defvar' does not change the variable's value
696 if it already has a value.\)
698 With argument, insert value in current buffer after the defun.
699 Return the result of evaluation."
701 (let ((debug-on-error eval-expression-debug-on-error
)
702 (print-length eval-expression-print-length
)
703 (print-level eval-expression-print-level
))
705 ;; Arrange for eval-region to "read" the (possibly) altered form.
706 ;; eval-region handles recording which file defines a function or
707 ;; variable. Re-written using `apply' to avoid capturing
708 ;; variables like `end'.
711 (let ((standard-output t
)
713 ;; Read the form from the buffer, and record where it ends.
718 (setq form
(read (current-buffer)))
720 ;; Alter the form if necessary.
721 (setq form
(eval-defun-1 (macroexpand form
)))
722 (list beg end standard-output
724 ;; Skipping to the end of the specified region
725 ;; will make eval-region return.
728 ;; The result of evaluation has been put onto VALUES. So return it.
731 (defun eval-defun (edebug-it)
732 "Evaluate the top-level form containing point, or after point.
734 If the current defun is actually a call to `defvar' or `defcustom',
735 evaluating it this way resets the variable using its initial value
736 expression even if the variable already has some other value.
737 \(Normally `defvar' and `defcustom' do not alter the value if there
738 already is one.) In an analogous way, evaluating a `defface'
739 overrides any customizations of the face, so that it becomes
740 defined exactly as the `defface' expression says.
742 If `eval-expression-debug-on-error' is non-nil, which is the default,
743 this command arranges for all errors to enter the debugger.
745 With a prefix argument, instrument the code for Edebug.
747 If acting on a `defun' for FUNCTION, and the function was
748 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
749 instrumented, just FUNCTION is printed.
751 If not acting on a `defun', the result of evaluation is displayed in
752 the minibuffer. This display is controlled by the variables
753 `eval-expression-print-length' and `eval-expression-print-level',
758 (eval-defun (not edebug-all-defs
)))
760 (if (null eval-expression-debug-on-error
)
762 (let ((old-value (make-symbol "t")) new-value value
)
763 (let ((debug-on-error old-value
))
764 (setq value
(eval-defun-2))
765 (setq new-value debug-on-error
))
766 (unless (eq old-value new-value
)
767 (setq debug-on-error new-value
))
770 ;; May still be used by some external Lisp-mode variant.
771 (define-obsolete-function-alias 'lisp-comment-indent
'comment-indent-default
)
773 ;; This function just forces a more costly detection of comments (using
774 ;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of
775 ;; taking a `;' inside a string started on another line for a comment starter.
776 ;; Note: `newcomment' gets it right now since we set comment-use-global-state
777 ;; so we could get rid of it. -stef
778 (defun lisp-mode-auto-fill ()
779 (if (> (current-column) (current-fill-column))
781 (nth 4 (syntax-ppss (point))))
783 (unless (and (boundp 'comment-auto-fill-only-comments
)
784 comment-auto-fill-only-comments
)
785 (let ((comment-start nil
) (comment-start-skip nil
))
788 (defvar lisp-indent-offset nil
789 "If non-nil, indent second line of expressions that many more columns.")
790 (defvar lisp-indent-function
'lisp-indent-function
)
792 (defun lisp-indent-line (&optional whole-exp
)
793 "Indent current line as Lisp code.
794 With argument, indent any additional lines of the same expression
795 rigidly along with this one."
797 (let ((indent (calculate-lisp-indent)) shift-amt end
798 (pos (- (point-max) (point)))
799 (beg (progn (beginning-of-line) (point))))
800 (skip-chars-forward " \t")
801 (if (or (null indent
) (looking-at "\\s<\\s<\\s<"))
802 ;; Don't alter indentation of a ;;; comment line
803 ;; or a line that starts in a string.
804 (goto-char (- (point-max) pos
))
805 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
806 ;; Single-semicolon comment lines should be indented
807 ;; as comment lines, not as code.
808 (progn (indent-for-comment) (forward-char -
1))
809 (if (listp indent
) (setq indent
(car indent
)))
810 (setq shift-amt
(- indent
(current-column)))
811 (if (zerop shift-amt
)
813 (delete-region beg
(point))
815 ;; If initial point was within line's indentation,
816 ;; position after the indentation. Else stay at same point in text.
817 (if (> (- (point-max) pos
) (point))
818 (goto-char (- (point-max) pos
)))
819 ;; If desired, shift remaining lines of expression the same amount.
820 (and whole-exp
(not (zerop shift-amt
))
829 (indent-code-rigidly beg end shift-amt
)))))
831 (defvar calculate-lisp-indent-last-sexp
)
833 (defun calculate-lisp-indent (&optional parse-start
)
834 "Return appropriate indentation for current line as Lisp code.
835 In usual case returns an integer: the column to indent to.
836 If the value is nil, that means don't change the indentation
837 because the line starts inside a string.
839 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
840 This means that following lines at the same level of indentation
841 should not necessarily be indented the same as this line.
842 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
843 is the buffer position of the start of the containing expression."
846 (let ((indent-point (point))
848 ;; setting this to a number inhibits calling hook
851 calculate-lisp-indent-last-sexp containing-sexp
)
853 (goto-char parse-start
)
854 (beginning-of-defun))
855 ;; Find outermost containing sexp
856 (while (< (point) indent-point
)
857 (setq state
(parse-partial-sexp (point) indent-point
0)))
858 ;; Find innermost containing sexp
861 (> (setq paren-depth
(elt state
0)) 0))
863 (setq calculate-lisp-indent-last-sexp
(elt state
2))
864 (setq containing-sexp
(elt state
1))
865 ;; Position following last unclosed open.
866 (goto-char (1+ containing-sexp
))
867 ;; Is there a complete sexp since then?
868 (if (and calculate-lisp-indent-last-sexp
869 (> calculate-lisp-indent-last-sexp
(point)))
870 ;; Yes, but is there a containing sexp after that?
871 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
873 (if (setq retry
(car (cdr peek
))) (setq state peek
)))))
876 ;; Innermost containing sexp found
877 (goto-char (1+ containing-sexp
))
878 (if (not calculate-lisp-indent-last-sexp
)
879 ;; indent-point immediately follows open paren.
881 (setq desired-indent
(current-column))
882 ;; Find the start of first element of containing sexp.
883 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
884 (cond ((looking-at "\\s(")
885 ;; First element of containing sexp is a list.
886 ;; Indent under that list.
888 ((> (save-excursion (forward-line 1) (point))
889 calculate-lisp-indent-last-sexp
)
890 ;; This is the first line to start within the containing sexp.
891 ;; It's almost certainly a function call.
892 (if (= (point) calculate-lisp-indent-last-sexp
)
893 ;; Containing sexp has nothing before this line
894 ;; except the first element. Indent under that element.
896 ;; Skip the first element, find start of second (the first
897 ;; argument of the function call) and indent under.
898 (progn (forward-sexp 1)
899 (parse-partial-sexp (point)
900 calculate-lisp-indent-last-sexp
902 (backward-prefix-chars))
904 ;; Indent beneath first sexp on same line as
905 ;; `calculate-lisp-indent-last-sexp'. Again, it's
906 ;; almost certainly a function call.
907 (goto-char calculate-lisp-indent-last-sexp
)
909 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
911 (backward-prefix-chars)))))
912 ;; Point is at the point to indent under unless we are inside a string.
913 ;; Call indentation hook except when overridden by lisp-indent-offset
914 ;; or if the desired indentation has already been computed.
915 (let ((normal-indent (current-column)))
917 ;; Inside a string, don't change indentation.
919 ((and (integerp lisp-indent-offset
) containing-sexp
)
920 ;; Indent by constant offset
921 (goto-char containing-sexp
)
922 (+ (current-column) lisp-indent-offset
))
923 ;; in this case calculate-lisp-indent-last-sexp is not nil
924 (calculate-lisp-indent-last-sexp
926 ;; try to align the parameters of a known function
927 (and lisp-indent-function
929 (funcall lisp-indent-function indent-point state
))
930 ;; If the function has no special alignment
931 ;; or it does not apply to this argument,
932 ;; try to align a constant-symbol under the last
933 ;; preceding constant symbol, if there is such one of
934 ;; the last 2 preceding symbols, in the previous
937 (goto-char indent-point
)
938 (skip-chars-forward " \t")
940 ;; The last sexp may not be at the indentation
941 ;; where it begins, so find that one, instead.
943 (goto-char calculate-lisp-indent-last-sexp
)
944 (while (and (not (looking-back "^[ \t]*"))
945 (or (not containing-sexp
)
946 (< (1+ containing-sexp
) (point))))
948 (backward-prefix-chars))
949 (setq calculate-lisp-indent-last-sexp
(point)))
950 (> calculate-lisp-indent-last-sexp
952 (goto-char (1+ containing-sexp
))
953 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
955 (let ((parse-sexp-ignore-comments t
)
957 (goto-char calculate-lisp-indent-last-sexp
)
958 (or (and (looking-at ":")
959 (setq indent
(current-column)))
960 (and (< (save-excursion (beginning-of-line) (point))
961 (prog2 (backward-sexp) (point)))
963 (setq indent
(current-column))))
965 ;; another symbols or constants not preceded by a constant
968 ;; in this case calculate-lisp-indent-last-sexp is nil
973 (defun lisp-indent-function (indent-point state
)
974 "This function is the normal value of the variable `lisp-indent-function'.
975 It is used when indenting a line within a function call, to see if the
976 called function says anything special about how to indent the line.
978 INDENT-POINT is the position where the user typed TAB, or equivalent.
979 Point is located at the point to indent under (for default indentation);
980 STATE is the `parse-partial-sexp' state for that position.
982 If the current line is in a call to a Lisp function
983 which has a non-nil property `lisp-indent-function',
984 that specifies how to do the indentation. The property value can be
985 * `defun', meaning indent `defun'-style;
986 * an integer N, meaning indent the first N arguments specially
987 like ordinary function arguments and then indent any further
988 arguments like a body;
989 * a function to call just as this function was called.
990 If that function returns nil, that means it doesn't specify
993 This function also returns nil meaning don't specify the indentation."
994 (let ((normal-indent (current-column)))
995 (goto-char (1+ (elt state
1)))
996 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
0 t
)
997 (if (and (elt state
2)
998 (not (looking-at "\\sw\\|\\s_")))
999 ;; car of form doesn't seem to be a symbol
1001 (if (not (> (save-excursion (forward-line 1) (point))
1002 calculate-lisp-indent-last-sexp
))
1003 (progn (goto-char calculate-lisp-indent-last-sexp
)
1005 (parse-partial-sexp (point)
1006 calculate-lisp-indent-last-sexp
0 t
)))
1007 ;; Indent under the list or under the first sexp on the same
1008 ;; line as calculate-lisp-indent-last-sexp. Note that first
1009 ;; thing on that line has to be complete sexp since we are
1010 ;; inside the innermost containing sexp.
1011 (backward-prefix-chars)
1013 (let ((function (buffer-substring (point)
1014 (progn (forward-sexp 1) (point))))
1016 (setq method
(or (get (intern-soft function
) 'lisp-indent-function
)
1017 (get (intern-soft function
) 'lisp-indent-hook
)))
1018 (cond ((or (eq method
'defun
)
1020 (> (length function
) 3)
1021 (string-match "\\`def" function
)))
1022 (lisp-indent-defform state indent-point
))
1024 (lisp-indent-specform method state
1025 indent-point normal-indent
))
1027 (funcall method indent-point state
)))))))
1029 (defvar lisp-body-indent
2
1030 "Number of columns to indent the second line of a `(def...)' form.")
1032 (defun lisp-indent-specform (count state indent-point normal-indent
)
1033 (let ((containing-form-start (elt state
1))
1035 body-indent containing-form-column
)
1036 ;; Move to the start of containing form, calculate indentation
1037 ;; to use for non-distinguished forms (> count), and move past the
1038 ;; function symbol. lisp-indent-function guarantees that there is at
1039 ;; least one word or symbol character following open paren of containing
1041 (goto-char containing-form-start
)
1042 (setq containing-form-column
(current-column))
1043 (setq body-indent
(+ lisp-body-indent containing-form-column
))
1046 ;; Now find the start of the last form.
1047 (parse-partial-sexp (point) indent-point
1 t
)
1048 (while (and (< (point) indent-point
)
1051 (setq count
(1- count
))
1053 (parse-partial-sexp (point) indent-point
1 t
))
1055 ;; Point is sitting on first character of last (or count) sexp.
1057 ;; A distinguished form. If it is the first or second form use double
1058 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1059 ;; to 2 (the default), this just happens to work the same with if as
1060 ;; the older code, but it makes unwind-protect, condition-case,
1061 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1062 ;; less hacked, behavior can be obtained by replacing below with
1063 ;; (list normal-indent containing-form-start).
1064 (if (<= (- i count
) 1)
1065 (list (+ containing-form-column
(* 2 lisp-body-indent
))
1066 containing-form-start
)
1067 (list normal-indent containing-form-start
))
1068 ;; A non-distinguished form. Use body-indent if there are no
1069 ;; distinguished forms and this is the first undistinguished form,
1070 ;; or if this is the first undistinguished form and the preceding
1071 ;; distinguished form has indentation at least as great as body-indent.
1072 (if (or (and (= i
0) (= count
0))
1073 (and (= count
0) (<= body-indent normal-indent
)))
1077 (defun lisp-indent-defform (state indent-point
)
1078 (goto-char (car (cdr state
)))
1080 (if (> (point) (car (cdr (cdr state
))))
1082 (goto-char (car (cdr state
)))
1083 (+ lisp-body-indent
(current-column)))))
1086 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1087 ;; like defun if the first form is placed on the next line, otherwise
1088 ;; it is indented like any other form (i.e. forms line up under first).
1090 (put 'lambda
'lisp-indent-function
'defun
)
1091 (put 'autoload
'lisp-indent-function
'defun
)
1092 (put 'progn
'lisp-indent-function
0)
1093 (put 'prog1
'lisp-indent-function
1)
1094 (put 'prog2
'lisp-indent-function
2)
1095 (put 'save-excursion
'lisp-indent-function
0)
1096 (put 'save-window-excursion
'lisp-indent-function
0)
1097 (put 'save-selected-window
'lisp-indent-function
0)
1098 (put 'save-restriction
'lisp-indent-function
0)
1099 (put 'save-match-data
'lisp-indent-function
0)
1100 (put 'save-current-buffer
'lisp-indent-function
0)
1101 (put 'with-current-buffer
'lisp-indent-function
1)
1102 (put 'combine-after-change-calls
'lisp-indent-function
0)
1103 (put 'with-output-to-string
'lisp-indent-function
0)
1104 (put 'with-temp-file
'lisp-indent-function
1)
1105 (put 'with-temp-buffer
'lisp-indent-function
0)
1106 (put 'with-temp-message
'lisp-indent-function
1)
1107 (put 'with-syntax-table
'lisp-indent-function
1)
1108 (put 'let
'lisp-indent-function
1)
1109 (put 'let
* 'lisp-indent-function
1)
1110 (put 'while
'lisp-indent-function
1)
1111 (put 'if
'lisp-indent-function
2)
1112 (put 'read-if
'lisp-indent-function
2)
1113 (put 'catch
'lisp-indent-function
1)
1114 (put 'condition-case
'lisp-indent-function
2)
1115 (put 'unwind-protect
'lisp-indent-function
1)
1116 (put 'with-output-to-temp-buffer
'lisp-indent-function
1)
1117 (put 'eval-after-load
'lisp-indent-function
1)
1118 (put 'dolist
'lisp-indent-function
1)
1119 (put 'dotimes
'lisp-indent-function
1)
1120 (put 'when
'lisp-indent-function
1)
1121 (put 'unless
'lisp-indent-function
1)
1123 (defun indent-sexp (&optional endpos
)
1124 "Indent each line of the list starting just after point.
1125 If optional arg ENDPOS is given, indent each line, stopping when
1126 ENDPOS is encountered."
1128 (let ((indent-stack (list nil
))
1130 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1131 ;; so that calculate-lisp-indent will find the beginning of
1132 ;; the defun we are in.
1133 ;; If ENDPOS is nil, it is safe not to scan before point
1134 ;; since every line we indent is more deeply nested than point is.
1135 (starting-point (if endpos nil
(point)))
1136 (last-point (point))
1137 last-depth bol outer-loop-done inner-loop-done state this-indent
)
1139 ;; Get error now if we don't have a complete sexp after point.
1140 (save-excursion (forward-sexp 1)))
1142 (setq outer-loop-done nil
)
1143 (while (if endpos
(< (point) endpos
)
1144 (not outer-loop-done
))
1145 (setq last-depth next-depth
1146 inner-loop-done nil
)
1147 ;; Parse this line so we can learn the state
1148 ;; to indent the next line.
1149 ;; This inner loop goes through only once
1150 ;; unless a line ends inside a string.
1151 (while (and (not inner-loop-done
)
1152 (not (setq outer-loop-done
(eobp))))
1153 (setq state
(parse-partial-sexp (point) (progn (end-of-line) (point))
1155 (setq next-depth
(car state
))
1156 ;; If the line contains a comment other than the sort
1157 ;; that is indented like code,
1158 ;; indent it now with indent-for-comment.
1159 ;; Comments indented like code are right already.
1160 ;; In any case clear the in-comment flag in the state
1161 ;; because parse-partial-sexp never sees the newlines.
1162 (if (car (nthcdr 4 state
))
1163 (progn (indent-for-comment)
1165 (setcar (nthcdr 4 state
) nil
)))
1166 ;; If this line ends inside a string,
1167 ;; go straight to next line, remaining within the inner loop,
1168 ;; and turn off the \-flag.
1169 (if (car (nthcdr 3 state
))
1172 (setcar (nthcdr 5 state
) nil
))
1173 (setq inner-loop-done t
)))
1177 (setq indent-stack
(nconc indent-stack
1178 (make-list (- next-depth
) nil
))
1179 last-depth
(- last-depth next-depth
)
1182 ;; Decide whether to exit.
1184 ;; If we have already reached the specified end,
1185 ;; give up and do not reindent this line.
1186 (if (<= endpos
(point))
1187 (setq outer-loop-done t
))
1188 ;; If no specified end, we are done if we have finished one sexp.
1189 (if (<= next-depth
0)
1190 (setq outer-loop-done t
)))
1191 (unless outer-loop-done
1192 (while (> last-depth next-depth
)
1193 (setq indent-stack
(cdr indent-stack
)
1194 last-depth
(1- last-depth
)))
1195 (while (< last-depth next-depth
)
1196 (setq indent-stack
(cons nil indent-stack
)
1197 last-depth
(1+ last-depth
)))
1198 ;; Now indent the next line according
1199 ;; to what we learned from parsing the previous one.
1201 (skip-chars-forward " \t")
1202 ;; But not if the line is blank, or just a comment
1203 ;; (except for double-semi comments; indent them as usual).
1204 (if (or (eobp) (looking-at "\\s<\\|\n"))
1206 (if (and (car indent-stack
)
1207 (>= (car indent-stack
) 0))
1208 (setq this-indent
(car indent-stack
))
1209 (let ((val (calculate-lisp-indent
1210 (if (car indent-stack
) (- (car indent-stack
))
1213 (setq this-indent val
)
1215 (setcar indent-stack
1216 (setq this-indent val
))
1217 (setcar indent-stack
(- (car (cdr val
))))
1218 (setq this-indent
(car val
))))))
1219 (if (and this-indent
(/= (current-column) this-indent
))
1220 (progn (delete-region bol
(point))
1221 (indent-to this-indent
)))))
1223 (setq outer-loop-done
(= (point) last-point
))
1224 (setq last-point
(point)))))))
1226 (defun lisp-indent-region (start end
)
1227 "Indent every line whose first char is between START and END inclusive."
1229 (let ((endmark (copy-marker end
)))
1231 (and (bolp) (not (eolp))
1233 (indent-sexp endmark
)
1234 (set-marker endmark nil
))))
1236 (defun indent-pp-sexp (&optional arg
)
1237 "Indent each line of the list starting just after point, or prettyprint it.
1238 A prefix argument specifies pretty-printing."
1243 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1245 (goto-char (point-max))
1246 (if (eq (char-before) ?
\n)
1247 (delete-char -
1)))))
1250 ;;;; Lisp paragraph filling commands.
1252 (defcustom emacs-lisp-docstring-fill-column
65
1253 "Value of `fill-column' to use when filling a docstring.
1254 Any non-integer value means do not use a different value of
1255 `fill-column' when filling docstrings."
1256 :type
'(choice (integer)
1257 (const :tag
"Use the current `fill-column'" t
))
1260 (defun lisp-fill-paragraph (&optional justify
)
1261 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1262 If any of the current line is a comment, fill the comment or the
1263 paragraph of it that point is in, preserving the comment's indentation
1264 and initial semicolons."
1266 (or (fill-comment-paragraph justify
)
1267 ;; Since fill-comment-paragraph returned nil, that means we're not in
1268 ;; a comment: Point is on a program line; we are interested
1269 ;; particularly in docstring lines.
1271 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1272 ;; are buffer-local, but we avoid changing them so that they can be set
1273 ;; to make `forward-paragraph' and friends do something the user wants.
1275 ;; `paragraph-start': The `(' in the character alternative and the
1276 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1277 ;; sexps and backquoted sexps that follow a docstring from being filled
1278 ;; with the docstring. This setting has the consequence of inhibiting
1279 ;; filling many program lines that are not docstrings, which is sensible,
1280 ;; because the user probably asked to fill program lines by accident, or
1281 ;; expecting indentation (perhaps we should try to do indenting in that
1282 ;; case). The `;' and `:' stop the paragraph being filled at following
1283 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1284 ;; escaped to keep font-locking, filling, & paren matching in the source
1287 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1288 ;; a docstring and identifies it as a paragraph separator, so that it
1289 ;; won't be filled. (Since the first line of documentation stands alone
1290 ;; in some contexts, filling should not alter the contents the author has
1291 ;; chosen.) Only the first line of a docstring begins with whitespace
1292 ;; and a quotation mark and ends with a period or (rarely) a comma.
1294 ;; The `fill-column' is temporarily bound to
1295 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1296 (let ((paragraph-start (concat paragraph-start
1297 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1299 (concat paragraph-separate
"\\|\\s-*\".*[,\\.]$"))
1300 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column
)
1301 (derived-mode-p 'emacs-lisp-mode
))
1302 emacs-lisp-docstring-fill-column
1304 (fill-paragraph justify
))
1305 ;; Never return nil.
1308 (defun indent-code-rigidly (start end arg
&optional nochange-regexp
)
1309 "Indent all lines of code, starting in the region, sideways by ARG columns.
1310 Does not affect lines starting inside comments or strings, assuming that
1311 the start of the region is not inside them.
1313 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1314 The last is a regexp which, if matched at the beginning of a line,
1315 means don't indent that line."
1316 (interactive "r\np")
1320 (setq end
(point-marker))
1323 (setq state
(parse-partial-sexp (point)
1325 (forward-line 1) (point))
1327 (while (< (point) end
)
1328 (or (car (nthcdr 3 state
))
1329 (and nochange-regexp
1330 (looking-at nochange-regexp
))
1331 ;; If line does not start in string, indent it
1332 (let ((indent (current-indentation)))
1333 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1335 (indent-to (max 0 (+ indent arg
)) 0))))
1336 (setq state
(parse-partial-sexp (point)
1338 (forward-line 1) (point))
1341 (provide 'lisp-mode
)
1343 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1344 ;;; lisp-mode.el ends here