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