1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009 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 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
27 ;; This mode is documented in the Emacs manual.
31 (defvar font-lock-comment-face
)
32 (defvar font-lock-doc-face
)
33 (defvar font-lock-keywords-case-fold-search
)
34 (defvar font-lock-string-face
)
36 (defvar lisp-mode-abbrev-table nil
)
38 (define-abbrev-table 'lisp-mode-abbrev-table
())
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
))
82 "Syntax table used in `emacs-lisp-mode'.")
84 (defvar lisp-mode-syntax-table
85 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table
)))
86 (modify-syntax-entry ?\
[ "_ " table
)
87 (modify-syntax-entry ?\
] "_ " table
)
88 (modify-syntax-entry ?
# "' 14b" table
)
89 (modify-syntax-entry ?|
"\" 23bn" table
)
91 "Syntax table used in `lisp-mode'.")
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 'deftype
'doc-string-elt
3)
138 (put 'defconst
'doc-string-elt
3)
139 (put 'defmacro
'doc-string-elt
3)
140 (put 'defmacro
* 'doc-string-elt
3)
141 (put 'defsubst
'doc-string-elt
3)
142 (put 'defstruct
'doc-string-elt
2)
143 (put 'define-skeleton
'doc-string-elt
2)
144 (put 'define-derived-mode
'doc-string-elt
4)
145 (put 'define-compilation-mode
'doc-string-elt
3)
146 (put 'easy-mmode-define-minor-mode
'doc-string-elt
2)
147 (put 'define-minor-mode
'doc-string-elt
2)
148 (put 'easy-mmode-define-global-mode
'doc-string-elt
2)
149 (put 'define-global-minor-mode
'doc-string-elt
2)
150 (put 'define-globalized-minor-mode
'doc-string-elt
2)
151 (put 'define-generic-mode
'doc-string-elt
7)
152 (put 'define-ibuffer-filter
'doc-string-elt
2)
153 (put 'define-ibuffer-op
'doc-string-elt
3)
154 (put 'define-ibuffer-sorter
'doc-string-elt
2)
155 (put 'lambda
'doc-string-elt
2)
156 (put 'defalias
'doc-string-elt
3)
157 (put 'defvaralias
'doc-string-elt
3)
158 (put 'define-category
'doc-string-elt
2)
159 (put 'define-overloadable-function
'doc-string-elt
3)
161 (defvar lisp-doc-string-elt-property
'doc-string-elt
162 "The symbol property that holds the docstring position info.")
164 (defun lisp-font-lock-syntactic-face-function (state)
166 ;; This might be a (doc)string or a |...| symbol.
167 (let ((startpos (nth 8 state
)))
168 (if (eq (char-after startpos
) ?|
)
169 ;; This is not a string, but a |...| symbol.
171 (let* ((listbeg (nth 1 state
))
172 (firstsym (and listbeg
175 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
177 (docelt (and firstsym
(get (intern-soft firstsym
)
178 lisp-doc-string-elt-property
))))
180 ;; It's a string in a form that can have a docstring.
181 ;; Check whether it's in docstring position.
183 (when (functionp docelt
)
184 (goto-char (match-end 1))
185 (setq docelt
(funcall docelt
)))
189 (while (and (> docelt
0) (< (point) startpos
)
190 (progn (forward-sexp 1) t
))
191 (setq docelt
(1- docelt
)))
193 (and (zerop docelt
) (<= (point) startpos
)
194 (progn (forward-comment (point-max)) t
)
195 (= (point) (nth 8 state
)))))
197 font-lock-string-face
))))
198 font-lock-comment-face
))
200 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive
)
201 "Common initialization routine for lisp modes.
202 The LISP-SYNTAX argument is used by code in inf-lisp.el and is
203 \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
204 score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for
205 font-lock keywords will not be case sensitive."
207 (set-syntax-table lisp-mode-syntax-table
))
208 (setq local-abbrev-table lisp-mode-abbrev-table
)
209 (make-local-variable 'paragraph-ignore-fill-prefix
)
210 (setq paragraph-ignore-fill-prefix t
)
211 (make-local-variable 'fill-paragraph-function
)
212 (setq fill-paragraph-function
'lisp-fill-paragraph
)
213 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
214 ;; a single docstring. Let's fix it here.
215 (set (make-local-variable 'adaptive-fill-function
)
216 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
217 ;; Adaptive fill mode gets in the way of auto-fill,
218 ;; and should make no difference for explicit fill
219 ;; because lisp-fill-paragraph should do the job.
220 ;; I believe that newcomment's auto-fill code properly deals with it -stef
221 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
222 (make-local-variable 'indent-line-function
)
223 (setq indent-line-function
'lisp-indent-line
)
224 (make-local-variable 'parse-sexp-ignore-comments
)
225 (setq parse-sexp-ignore-comments t
)
226 (make-local-variable 'outline-regexp
)
227 (setq outline-regexp
";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
228 (make-local-variable 'outline-level
)
229 (setq outline-level
'lisp-outline-level
)
230 (make-local-variable 'comment-start
)
231 (setq comment-start
";")
232 (make-local-variable 'comment-start-skip
)
233 ;; Look within the line for a ; following an even number of backslashes
234 ;; after either a non-backslash or the line beginning.
235 (setq comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
236 (make-local-variable 'font-lock-comment-start-skip
)
237 ;; Font lock mode uses this only when it KNOWS a comment is starting.
238 (setq font-lock-comment-start-skip
";+ *")
239 (make-local-variable 'comment-add
)
240 (setq comment-add
1) ;default to `;;' in comment-region
241 (make-local-variable 'comment-column
)
242 (setq comment-column
40)
243 ;; Don't get confused by `;' in doc strings when paragraph-filling.
244 (set (make-local-variable 'comment-use-global-state
) t
)
245 (make-local-variable 'imenu-generic-expression
)
246 (setq imenu-generic-expression lisp-imenu-generic-expression
)
247 (make-local-variable 'multibyte-syntax-as-symbol
)
248 (setq multibyte-syntax-as-symbol t
)
249 (set (make-local-variable 'syntax-begin-function
) 'beginning-of-defun
)
250 (setq font-lock-defaults
251 `((lisp-font-lock-keywords
252 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2
)
253 nil
,keywords-case-insensitive
(("+-*/.<>=!?$%_&~^:@" .
"w")) nil
254 (font-lock-mark-block-function . mark-defun
)
255 (font-lock-syntactic-face-function
256 . lisp-font-lock-syntactic-face-function
))))
258 (defun lisp-outline-level ()
259 "Lisp mode `outline-level' function."
260 (let ((len (- (match-end 0) (match-beginning 0))))
261 (if (looking-at "(\\|;;;###autoload")
265 (defvar lisp-mode-shared-map
266 (let ((map (make-sparse-keymap)))
267 (define-key map
"\e\C-q" 'indent-sexp
)
268 (define-key map
"\177" 'backward-delete-char-untabify
)
269 ;; This gets in the way when viewing a Lisp file in view-mode. As
270 ;; long as [backspace] is mapped into DEL via the
271 ;; function-key-map, this should remain disabled!!
272 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
274 "Keymap for commands shared by all sorts of Lisp modes.")
276 (defvar emacs-lisp-mode-map
277 (let ((map (make-sparse-keymap "Emacs-Lisp"))
278 (menu-map (make-sparse-keymap "Emacs-Lisp"))
279 (lint-map (make-sparse-keymap))
280 (prof-map (make-sparse-keymap))
281 (tracing-map (make-sparse-keymap)))
282 (set-keymap-parent map lisp-mode-shared-map
)
283 (define-key map
"\e\t" 'lisp-complete-symbol
)
284 (define-key map
"\e\C-x" 'eval-defun
)
285 (define-key map
"\e\C-q" 'indent-pp-sexp
)
286 (define-key map
[menu-bar emacs-lisp
] (cons (purecopy "Emacs-Lisp") menu-map
))
287 (define-key menu-map
[eldoc]
288 `(menu-item ,(purecopy "Auto-Display Documentation Strings") eldoc-mode
289 :button (:toggle . (bound-and-true-p eldoc-mode))
290 :help ,(purecopy "Display the documentation string for the item under cursor")))
291 (define-key menu-map [checkdoc]
292 `(menu-item ,(purecopy "Check Documentation Strings") checkdoc
293 :help ,(purecopy "Check documentation strings for style requirements")))
294 (define-key menu-map [re-builder]
295 `(menu-item ,(purecopy "Construct Regexp") re-builder
296 :help ,(purecopy "Construct a regexp interactively")))
297 (define-key menu-map [tracing] (cons (purecopy "Tracing") tracing-map))
298 (define-key tracing-map [tr-a]
299 `(menu-item ,(purecopy "Untrace all") untrace-all
300 :help ,(purecopy "Untrace all currently traced functions")))
301 (define-key tracing-map [tr-uf]
302 `(menu-item ,(purecopy "Untrace function...") untrace-function
303 :help ,(purecopy "Untrace function, and possibly activate all remaining advice")))
304 (define-key tracing-map [tr-sep] '("--"))
305 (define-key tracing-map [tr-q]
306 `(menu-item ,(purecopy "Trace function quietly...") trace-function-background
307 :help ,(purecopy "Trace the function with trace output going quietly to a buffer")))
308 (define-key tracing-map [tr-f]
309 `(menu-item ,(purecopy "Trace function...") trace-function
310 :help ,(purecopy "Trace the function given as an argument")))
311 (define-key menu-map [profiling] (cons (purecopy "Profiling") prof-map))
312 (define-key prof-map [prof-restall]
313 `(menu-item ,(purecopy "Remove Instrumentation for All Functions") elp-restore-all
314 :help ,(purecopy "Restore the original definitions of all functions being profiled")))
315 (define-key prof-map [prof-restfunc]
316 `(menu-item ,(purecopy "Remove Instrumentation for Function...") elp-restore-function
317 :help ,(purecopy "Restore an instrumented function to its original definition")))
319 (define-key prof-map [sep-rem] '("--"))
320 (define-key prof-map [prof-resall]
321 `(menu-item ,(purecopy "Reset Counters for All Functions") elp-reset-all
322 :help ,(purecopy "Reset the profiling information for all functions being profiled")))
323 (define-key prof-map [prof-resfunc]
324 `(menu-item ,(purecopy "Reset Counters for Function...") elp-reset-function
325 :help ,(purecopy "Reset the profiling information for a function")))
326 (define-key prof-map [prof-res]
327 `(menu-item ,(purecopy "Show Profiling Results") elp-results
328 :help ,(purecopy "Display current profiling results")))
329 (define-key prof-map [prof-pack]
330 `(menu-item ,(purecopy "Instrument Package...") elp-instrument-package
331 :help ,(purecopy "Instrument for profiling all function that start with a prefix")))
332 (define-key prof-map [prof-func]
333 `(menu-item ,(purecopy "Instrument Function...") elp-instrument-function
334 :help ,(purecopy "Instrument a function for profiling")))
335 (define-key menu-map [lint] (cons (purecopy "Linting") lint-map))
336 (define-key lint-map [lint-di]
337 `(menu-item ,(purecopy "Lint Directory...") elint-directory
338 :help ,(purecopy "Lint a directory")))
339 (define-key lint-map [lint-f]
340 `(menu-item ,(purecopy "Lint File...") elint-file
341 :help ,(purecopy "Lint a file")))
342 (define-key lint-map [lint-b]
343 `(menu-item ,(purecopy "Lint Buffer") elint-current-buffer
344 :help ,(purecopy "Lint the current buffer")))
345 (define-key lint-map [lint-d]
346 `(menu-item ,(purecopy "Lint Defun") elint-defun
347 :help ,(purecopy "Lint the function at point")))
348 (define-key menu-map [edebug-defun]
349 `(menu-item ,(purecopy "Instrument Function for Debugging") edebug-defun
350 :help ,(purecopy "Evaluate the top level form point is in, stepping through with Edebug")
351 :keys ,(purecopy "C-u C-M-x")))
352 (define-key menu-map [separator-byte] '("--"))
353 (define-key menu-map [disas]
354 `(menu-item ,(purecopy "Disassemble byte compiled object...") disassemble
355 :help ,(purecopy "Print disassembled code for OBJECT in a buffer")))
356 (define-key menu-map [byte-recompile]
357 `(menu-item ,(purecopy "Byte-recompile Directory...") byte-recompile-directory
358 :help ,(purecopy "Recompile every `.el' file in DIRECTORY that needs recompilation")))
359 (define-key menu-map [emacs-byte-compile-and-load]
360 `(menu-item ,(purecopy "Byte-compile And Load") emacs-lisp-byte-compile-and-load
361 :help ,(purecopy "Byte-compile the current file (if it has changed), then load compiled code")))
362 (define-key menu-map [byte-compile]
363 `(menu-item ,(purecopy "Byte-compile This File") emacs-lisp-byte-compile
364 :help ,(purecopy "Byte compile the file containing the current buffer")))
365 (define-key menu-map [separator-eval] '("--"))
366 (define-key menu-map [ielm]
367 `(menu-item ,(purecopy "Interactive Expression Evaluation") ielm
368 :help ,(purecopy "Interactively evaluate Emacs Lisp expressions")))
369 (define-key menu-map [eval-buffer]
370 `(menu-item ,(purecopy "Evaluate Buffer") eval-buffer
371 :help ,(purecopy "Execute the current buffer as Lisp code")))
372 (define-key menu-map [eval-region]
373 `(menu-item ,(purecopy "Evaluate Region") eval-region
374 :help ,(purecopy "Execute the region as Lisp code")
375 :enable mark-active))
376 (define-key menu-map [eval-sexp]
377 `(menu-item ,(purecopy "Evaluate Last S-expression") eval-last-sexp
378 :help ,(purecopy "Evaluate sexp before point; print value in minibuffer")))
379 (define-key menu-map [separator-format] '("--"))
380 (define-key menu-map [comment-region]
381 `(menu-item ,(purecopy "Comment Out Region") comment-region
382 :help ,(purecopy "Comment or uncomment each line in the region")
383 :enable mark-active))
384 (define-key menu-map [indent-region]
385 `(menu-item ,(purecopy "Indent Region") indent-region
386 :help ,(purecopy "Indent each nonblank line in the region")
387 :enable mark-active))
388 (define-key menu-map [indent-line] '("Indent Line" . lisp-indent-line))
390 "Keymap for Emacs Lisp mode.
391 All commands in `lisp-mode-shared-map' are inherited by this map.")
393 (defun emacs-lisp-byte-compile ()
394 "Byte compile the file containing the current buffer."
397 (byte-compile-file buffer-file-name)
398 (error "The buffer must be saved in a file first")))
400 (defun emacs-lisp-byte-compile-and-load ()
401 "Byte-compile the current file (if it has changed), then load compiled code."
404 (error "The buffer must be saved in a file first"))
406 ;; Recompile if file or buffer has changed since last compilation.
407 (if (and (buffer-modified-p)
408 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
410 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
411 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
412 (load-file compiled-file-name)
413 (byte-compile-file buffer-file-name t))))
415 (defcustom emacs-lisp-mode-hook nil
416 "Hook run when entering Emacs Lisp mode."
417 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
421 (defcustom lisp-mode-hook nil
422 "Hook run when entering Lisp mode."
423 :options '(imenu-add-menubar-index)
427 (defcustom lisp-interaction-mode-hook nil
428 "Hook run when entering Lisp Interaction mode."
429 :options '(turn-on-eldoc-mode)
433 (defun emacs-lisp-mode ()
434 "Major mode for editing Lisp code to run in Emacs.
436 Delete converts tabs to spaces as it moves back.
437 Blank lines separate paragraphs. Semicolons start comments.
439 \\{emacs-lisp-mode-map}
440 Entry to this mode calls the value of `emacs-lisp-mode-hook'
441 if that value is non-nil."
443 (kill-all-local-variables)
444 (use-local-map emacs-lisp-mode-map)
445 (set-syntax-table emacs-lisp-mode-syntax-table)
446 (setq major-mode 'emacs-lisp-mode)
447 (setq mode-name "Emacs-Lisp")
448 (lisp-mode-variables)
449 (setq imenu-case-fold-search nil)
450 (run-mode-hooks 'emacs-lisp-mode-hook))
451 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
453 (defvar lisp-mode-map
454 (let ((map (make-sparse-keymap))
455 (menu-map (make-sparse-keymap "Lisp")))
456 (set-keymap-parent map lisp-mode-shared-map)
457 (define-key map "\e\C-x" 'lisp-eval-defun)
458 (define-key map "\C-c\C-z" 'run-lisp)
459 (define-key map [menu-bar lisp] (cons (purecopy "Lisp") menu-map))
460 (define-key menu-map [run-lisp]
461 `(menu-item ,(purecopy "Run inferior Lisp") run-lisp
462 :help ,(purecopy "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'")))
463 (define-key menu-map [ev-def]
464 `(menu-item ,(purecopy "Eval defun") lisp-eval-defun
465 :help ,(purecopy "Send the current defun to the Lisp process made by M-x run-lisp")))
466 (define-key menu-map [ind-sexp]
467 `(menu-item ,(purecopy "Indent sexp") indent-sexp
468 :help ,(purecopy "Indent each line of the list starting just after point")))
470 "Keymap for ordinary Lisp mode.
471 All commands in `lisp-mode-shared-map' are inherited by this map.")
474 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
476 Delete converts tabs to spaces as it moves back.
477 Blank lines separate paragraphs. Semicolons start comments.
480 Note that `run-lisp' may be used either to start an inferior Lisp job
481 or to switch back to an existing one.
483 Entry to this mode calls the value of `lisp-mode-hook'
484 if that value is non-nil."
486 (kill-all-local-variables)
487 (use-local-map lisp-mode-map)
488 (setq major-mode 'lisp-mode)
489 (setq mode-name "Lisp")
490 (lisp-mode-variables nil t)
491 (make-local-variable 'comment-start-skip)
492 (setq comment-start-skip
493 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
494 (setq imenu-case-fold-search t)
495 (set-syntax-table lisp-mode-syntax-table)
496 (run-mode-hooks 'lisp-mode-hook))
497 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
499 (defun lisp-find-tag-default ()
500 (let ((default (find-tag-default)))
501 (when (stringp default)
502 (if (string-match ":+" default)
503 (substring default (match-end 0))
506 ;; Used in old LispM code.
507 (defalias 'common-lisp-mode 'lisp-mode)
509 ;; This will do unless inf-lisp.el is loaded.
510 (defun lisp-eval-defun (&optional and-go)
511 "Send the current defun to the Lisp process made by \\[run-lisp]."
513 (error "Process lisp does not exist"))
515 (defvar lisp-interaction-mode-map
516 (let ((map (make-sparse-keymap))
517 (menu-map (make-sparse-keymap "Lisp-Interaction")))
518 (set-keymap-parent map lisp-mode-shared-map)
519 (define-key map "\e\C-x" 'eval-defun)
520 (define-key map "\e\C-q" 'indent-pp-sexp)
521 (define-key map "\e\t" 'lisp-complete-symbol)
522 (define-key map "\n" 'eval-print-last-sexp)
523 (define-key map [menu-bar lisp-interaction] (cons (purecopy "Lisp-Interaction") menu-map))
524 (define-key menu-map [eval-defun]
525 `(menu-item ,(purecopy "Evaluate Defun") eval-defun
526 :help ,(purecopy "Evaluate the top-level form containing point, or after point")))
527 (define-key menu-map [eval-print-last-sexp]
528 `(menu-item ,(purecopy "Evaluate and print") eval-print-last-sexp
529 :help ,(purecopy "Evaluate sexp before point; print value into current buffer")))
530 (define-key menu-map [edebug-defun-lisp-interaction]
531 `(menu-item ,(purecopy "Instrument Function for Debugging") edebug-defun
532 :help ,(purecopy "Evaluate the top level form point is in, stepping through with Edebug")
533 :keys ,(purecopy "C-u C-M-x")))
534 (define-key menu-map [indent-pp-sexp]
535 `(menu-item ,(purecopy "Indent or Pretty-Print") indent-pp-sexp
536 :help ,(purecopy "Indent each line of the list starting just after point, or prettyprint it")))
537 (define-key menu-map [lisp-complete-symbol]
538 `(menu-item ,(purecopy "Complete Lisp Symbol") lisp-complete-symbol
539 :help ,(purecopy "Perform completion on Lisp symbol preceding point")))
541 "Keymap for Lisp Interaction mode.
542 All commands in `lisp-mode-shared-map' are inherited by this map.")
544 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
545 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
546 "Major mode for typing and evaluating Lisp forms.
547 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
548 before point, and prints its value into the buffer, advancing point.
549 Note that printing is controlled by `eval-expression-print-length'
550 and `eval-expression-print-level'.
553 Delete converts tabs to spaces as it moves back.
554 Paragraphs are separated only by blank lines.
555 Semicolons start comments.
557 \\{lisp-interaction-mode-map}
558 Entry to this mode calls the value of `lisp-interaction-mode-hook'
559 if that value is non-nil.")
561 (defun eval-print-last-sexp ()
562 "Evaluate sexp before point; print value into current buffer.
564 If `eval-expression-debug-on-error' is non-nil, which is the default,
565 this command arranges for all errors to enter the debugger.
567 Note that printing the result is controlled by the variables
568 `eval-expression-print-length' and `eval-expression-print-level',
571 (let ((standard-output (current-buffer)))
577 (defun last-sexp-setup-props (beg end value alt1 alt2)
578 "Set up text properties for the output of `eval-last-sexp-1'.
579 BEG and END are the start and end of the output in current-buffer.
580 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
581 alternative printed representations that can be displayed."
582 (let ((map (make-sparse-keymap)))
583 (define-key map "\C-m" 'last-sexp-toggle-display)
584 (define-key map [down-mouse-2] 'mouse-set-point)
585 (define-key map [mouse-2] 'last-sexp-toggle-display)
588 `(printed-value (,value ,alt1 ,alt2)
591 help-echo "RET, mouse-2: toggle abbreviated display"
592 rear-nonsticky (mouse-face keymap help-echo
596 (defun last-sexp-toggle-display (&optional arg)
597 "Toggle between abbreviated and unabbreviated printed representations."
601 (let ((value (get-text-property (point) 'printed-value)))
603 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
606 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
607 (standard-output (current-buffer))
609 (delete-region beg end)
610 (insert (nth 1 value))
612 (setq point (1- (point))))
613 (last-sexp-setup-props beg (point)
617 (goto-char (min (point-max) point)))))))
619 (defun prin1-char (char)
620 "Return a string representing CHAR as a character rather than as an integer.
621 If CHAR is not a character, return nil."
624 (let ((c (event-basic-type char))
625 (mods (event-modifiers char))
627 ;; Prevent ?A from turning into ?\S-a.
628 (if (and (memq 'shift mods)
629 (zerop (logand char ?\S-\^@))
630 (not (let ((case-fold-search nil))
631 (char-equal c (upcase c)))))
632 (setq c (upcase c) mods nil))
633 ;; What string are we considering using?
640 (cond ((eq modif 'super) "\\s-")
641 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
644 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
649 ;; Verify the string reads a CHAR, not to some other character.
650 ;; If it doesn't, return nil instead.
652 (= (car (read-from-string string)) char)
656 (defun preceding-sexp ()
657 "Return sexp before the point."
658 (let ((opoint (point))
662 (with-syntax-table emacs-lisp-mode-syntax-table
663 ;; If this sexp appears to be enclosed in `...'
664 ;; then ignore the surrounding quotes.
666 (or (eq (following-char) ?\')
667 (eq (preceding-char) ?\')))
669 ;; If we were after `?\e' (or similar case),
670 ;; use the whole thing, not just the `e'.
671 (when (eq (preceding-char) ?\\)
673 (when (eq (preceding-char) ??)
676 ;; Skip over hash table read syntax.
677 (and (> (point) (1+ (point-min)))
678 (looking-back "#s" (- (point) 2))
682 (when (eq (preceding-char) ?=)
685 (skip-chars-backward "0-9#=")
686 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
691 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
692 ;; `variable' so that the value is returned, not the
694 (if (and ignore-quotes
695 (eq (following-char) ?`))
697 (narrow-to-region (point-min) opoint)
698 (setq expr (read (current-buffer)))
699 ;; If it's an (interactive ...) form, it's more
700 ;; useful to show how an interactive call would
703 (eq (car expr) 'interactive)
705 (list 'call-interactively
714 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
715 "Evaluate sexp before point; print value in minibuffer.
716 With argument, print output into current buffer."
717 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
718 (eval-last-sexp-print-value (eval (preceding-sexp)))))
721 (defun eval-last-sexp-print-value (value)
722 (let ((unabbreviated (let ((print-length nil) (print-level nil))
723 (prin1-to-string value)))
724 (print-length eval-expression-print-length)
725 (print-level eval-expression-print-level)
730 (let ((str (eval-expression-print-format value)))
731 (if str (princ str)))
733 (when (and (bufferp standard-output)
734 (or (not (null print-length))
735 (not (null print-level)))
736 (not (string= unabbreviated
737 (buffer-substring-no-properties beg end))))
738 (last-sexp-setup-props beg end value
740 (buffer-substring-no-properties beg end))
744 (defvar eval-last-sexp-fake-value (make-symbol "t"))
746 (defun eval-last-sexp (eval-last-sexp-arg-internal)
747 "Evaluate sexp before point; print value in minibuffer.
748 Interactively, with prefix argument, print output into current buffer.
749 Truncates long output according to the value of the variables
750 `eval-expression-print-length' and `eval-expression-print-level'.
752 If `eval-expression-debug-on-error' is non-nil, which is the default,
753 this command arranges for all errors to enter the debugger."
755 (if (null eval-expression-debug-on-error)
756 (eval-last-sexp-1 eval-last-sexp-arg-internal)
758 (let ((debug-on-error eval-last-sexp-fake-value))
759 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
761 (unless (eq (cdr value) eval-last-sexp-fake-value)
762 (setq debug-on-error (cdr value)))
765 (defun eval-defun-1 (form)
766 "Treat some expressions specially.
767 Reset the `defvar' and `defcustom' variables to the initial value.
768 Reinitialize the face according to the `defface' specification."
769 ;; The code in edebug-defun should be consistent with this, but not
770 ;; the same, since this gets a macroexpended form.
771 (cond ((not (listp form))
773 ((and (eq (car form) 'defvar)
774 (cdr-safe (cdr-safe form))
775 (boundp (cadr form)))
776 ;; Force variable to be re-set.
777 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
778 (setq-default ,(nth 1 form) ,(nth 2 form))))
779 ;; `defcustom' is now macroexpanded to
780 ;; `custom-declare-variable' with a quoted value arg.
781 ((and (eq (car form) 'custom-declare-variable)
782 (default-boundp (eval (nth 1 form))))
783 ;; Force variable to be bound.
784 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
786 ;; `defface' is macroexpanded to `custom-declare-face'.
787 ((eq (car form) 'custom-declare-face)
789 (setq face-new-frame-defaults
790 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
791 (put (eval (nth 1 form)) 'face-defface-spec nil)
792 ;; Setting `customized-face' to the new spec after calling
793 ;; the form, but preserving the old saved spec in `saved-face',
794 ;; imitates the situation when the new face spec is set
795 ;; temporarily for the current session in the customize
796 ;; buffer, thus allowing `face-user-default-spec' to use the
797 ;; new customized spec instead of the saved spec.
798 ;; Resetting `saved-face' temporarily to nil is needed to let
799 ;; `defface' change the spec, regardless of a saved spec.
801 (put ,(nth 1 form) 'saved-face
802 ',(get (eval (nth 1 form)) 'saved-face))
803 (put ,(nth 1 form) 'customized-face
805 (put (eval (nth 1 form)) 'saved-face nil)))
806 ((eq (car form) 'progn)
807 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
810 (defun eval-defun-2 ()
811 "Evaluate defun that point is in or before.
812 The value is displayed in the minibuffer.
813 If the current defun is actually a call to `defvar',
814 then reset the variable using the initial value expression
815 even if the variable already has some other value.
816 \(Normally `defvar' does not change the variable's value
817 if it already has a value.\)
819 With argument, insert value in current buffer after the defun.
820 Return the result of evaluation."
822 ;; FIXME: the print-length/level bindings should only be applied while
823 ;; printing, not while evaluating.
824 (let ((debug-on-error eval-expression-debug-on-error)
825 (print-length eval-expression-print-length)
826 (print-level eval-expression-print-level))
828 ;; Arrange for eval-region to "read" the (possibly) altered form.
829 ;; eval-region handles recording which file defines a function or
830 ;; variable. Re-written using `apply' to avoid capturing
831 ;; variables like `end'.
834 (let ((standard-output t)
836 ;; Read the form from the buffer, and record where it ends.
841 (setq form (read (current-buffer)))
843 ;; Alter the form if necessary.
844 (setq form (eval-defun-1 (macroexpand form)))
845 (list beg end standard-output
847 ;; Skipping to the end of the specified region
848 ;; will make eval-region return.
851 ;; The result of evaluation has been put onto VALUES. So return it.
854 (defun eval-defun (edebug-it)
855 "Evaluate the top-level form containing point, or after point.
857 If the current defun is actually a call to `defvar' or `defcustom',
858 evaluating it this way resets the variable using its initial value
859 expression even if the variable already has some other value.
860 \(Normally `defvar' and `defcustom' do not alter the value if there
861 already is one.) In an analogous way, evaluating a `defface'
862 overrides any customizations of the face, so that it becomes
863 defined exactly as the `defface' expression says.
865 If `eval-expression-debug-on-error' is non-nil, which is the default,
866 this command arranges for all errors to enter the debugger.
868 With a prefix argument, instrument the code for Edebug.
870 If acting on a `defun' for FUNCTION, and the function was
871 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
872 instrumented, just FUNCTION is printed.
874 If not acting on a `defun', the result of evaluation is displayed in
875 the minibuffer. This display is controlled by the variables
876 `eval-expression-print-length' and `eval-expression-print-level',
881 (eval-defun (not edebug-all-defs)))
883 (if (null eval-expression-debug-on-error)
885 (let ((old-value (make-symbol "t")) new-value value)
886 (let ((debug-on-error old-value))
887 (setq value (eval-defun-2))
888 (setq new-value debug-on-error))
889 (unless (eq old-value new-value)
890 (setq debug-on-error new-value))
893 ;; May still be used by some external Lisp-mode variant.
894 (define-obsolete-function-alias 'lisp-comment-indent
895 'comment-indent-default "22.1")
896 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
898 (defcustom lisp-indent-offset nil
899 "If non-nil, indent second line of expressions that many more columns."
901 :type '(choice (const nil) integer))
902 (put 'lisp-indent-offset 'safe-local-variable
903 (lambda (x) (or (null x) (integerp x))))
905 (defcustom lisp-indent-function 'lisp-indent-function
906 "A function to be called by `calculate-lisp-indent'.
907 It indents the arguments of a Lisp function call. This function
908 should accept two arguments: the indent-point, and the
909 `parse-partial-sexp' state at that position. One option for this
910 function is `common-lisp-indent-function'."
914 (defun lisp-indent-line (&optional whole-exp)
915 "Indent current line as Lisp code.
916 With argument, indent any additional lines of the same expression
917 rigidly along with this one."
919 (let ((indent (calculate-lisp-indent)) shift-amt end
920 (pos (- (point-max) (point)))
921 (beg (progn (beginning-of-line) (point))))
922 (skip-chars-forward " \t")
923 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
924 ;; Don't alter indentation of a ;;; comment line
925 ;; or a line that starts in a string.
926 (goto-char (- (point-max) pos))
927 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
928 ;; Single-semicolon comment lines should be indented
929 ;; as comment lines, not as code.
930 (progn (indent-for-comment) (forward-char -1))
931 (if (listp indent) (setq indent (car indent)))
932 (setq shift-amt (- indent (current-column)))
933 (if (zerop shift-amt)
935 (delete-region beg (point))
937 ;; If initial point was within line's indentation,
938 ;; position after the indentation. Else stay at same point in text.
939 (if (> (- (point-max) pos) (point))
940 (goto-char (- (point-max) pos)))
941 ;; If desired, shift remaining lines of expression the same amount.
942 (and whole-exp (not (zerop shift-amt))
951 (indent-code-rigidly beg end shift-amt)))))
953 (defvar calculate-lisp-indent-last-sexp)
955 (defun calculate-lisp-indent (&optional parse-start)
956 "Return appropriate indentation for current line as Lisp code.
957 In usual case returns an integer: the column to indent to.
958 If the value is nil, that means don't change the indentation
959 because the line starts inside a string.
961 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
962 This means that following lines at the same level of indentation
963 should not necessarily be indented the same as this line.
964 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
965 is the buffer position of the start of the containing expression."
968 (let ((indent-point (point))
970 ;; setting this to a number inhibits calling hook
973 calculate-lisp-indent-last-sexp containing-sexp)
975 (goto-char parse-start)
976 (beginning-of-defun))
977 ;; Find outermost containing sexp
978 (while (< (point) indent-point)
979 (setq state (parse-partial-sexp (point) indent-point 0)))
980 ;; Find innermost containing sexp
983 (> (setq paren-depth (elt state 0)) 0))
985 (setq calculate-lisp-indent-last-sexp (elt state 2))
986 (setq containing-sexp (elt state 1))
987 ;; Position following last unclosed open.
988 (goto-char (1+ containing-sexp))
989 ;; Is there a complete sexp since then?
990 (if (and calculate-lisp-indent-last-sexp
991 (> calculate-lisp-indent-last-sexp (point)))
992 ;; Yes, but is there a containing sexp after that?
993 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
995 (if (setq retry (car (cdr peek))) (setq state peek)))))
998 ;; Innermost containing sexp found
999 (goto-char (1+ containing-sexp))
1000 (if (not calculate-lisp-indent-last-sexp)
1001 ;; indent-point immediately follows open paren.
1003 (setq desired-indent (current-column))
1004 ;; Find the start of first element of containing sexp.
1005 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1006 (cond ((looking-at "\\s(")
1007 ;; First element of containing sexp is a list.
1008 ;; Indent under that list.
1010 ((> (save-excursion (forward-line 1) (point))
1011 calculate-lisp-indent-last-sexp)
1012 ;; This is the first line to start within the containing sexp.
1013 ;; It's almost certainly a function call.
1014 (if (= (point) calculate-lisp-indent-last-sexp)
1015 ;; Containing sexp has nothing before this line
1016 ;; except the first element. Indent under that element.
1018 ;; Skip the first element, find start of second (the first
1019 ;; argument of the function call) and indent under.
1020 (progn (forward-sexp 1)
1021 (parse-partial-sexp (point)
1022 calculate-lisp-indent-last-sexp
1024 (backward-prefix-chars))
1026 ;; Indent beneath first sexp on same line as
1027 ;; `calculate-lisp-indent-last-sexp'. Again, it's
1028 ;; almost certainly a function call.
1029 (goto-char calculate-lisp-indent-last-sexp)
1031 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
1033 (backward-prefix-chars)))))
1034 ;; Point is at the point to indent under unless we are inside a string.
1035 ;; Call indentation hook except when overridden by lisp-indent-offset
1036 ;; or if the desired indentation has already been computed.
1037 (let ((normal-indent (current-column)))
1038 (cond ((elt state 3)
1039 ;; Inside a string, don't change indentation.
1041 ((and (integerp lisp-indent-offset) containing-sexp)
1042 ;; Indent by constant offset
1043 (goto-char containing-sexp)
1044 (+ (current-column) lisp-indent-offset))
1045 ;; in this case calculate-lisp-indent-last-sexp is not nil
1046 (calculate-lisp-indent-last-sexp
1048 ;; try to align the parameters of a known function
1049 (and lisp-indent-function
1051 (funcall lisp-indent-function indent-point state))
1052 ;; If the function has no special alignment
1053 ;; or it does not apply to this argument,
1054 ;; try to align a constant-symbol under the last
1055 ;; preceding constant symbol, if there is such one of
1056 ;; the last 2 preceding symbols, in the previous
1057 ;; uncommented line.
1058 (and (save-excursion
1059 (goto-char indent-point)
1060 (skip-chars-forward " \t")
1062 ;; The last sexp may not be at the indentation
1063 ;; where it begins, so find that one, instead.
1065 (goto-char calculate-lisp-indent-last-sexp)
1066 ;; Handle prefix characters and whitespace
1067 ;; following an open paren. (Bug#1012)
1068 (backward-prefix-chars)
1069 (while (and (not (looking-back "^[ \t]*\\|([ \t]+"))
1070 (or (not containing-sexp)
1071 (< (1+ containing-sexp) (point))))
1073 (backward-prefix-chars))
1074 (setq calculate-lisp-indent-last-sexp (point)))
1075 (> calculate-lisp-indent-last-sexp
1077 (goto-char (1+ containing-sexp))
1078 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1080 (let ((parse-sexp-ignore-comments t)
1082 (goto-char calculate-lisp-indent-last-sexp)
1083 (or (and (looking-at ":")
1084 (setq indent (current-column)))
1085 (and (< (save-excursion (beginning-of-line) (point))
1086 (prog2 (backward-sexp) (point)))
1088 (setq indent (current-column))))
1090 ;; another symbols or constants not preceded by a constant
1091 ;; as defined above.
1093 ;; in this case calculate-lisp-indent-last-sexp is nil
1098 (defun lisp-indent-function (indent-point state)
1099 "This function is the normal value of the variable `lisp-indent-function'.
1100 It is used when indenting a line within a function call, to see if the
1101 called function says anything special about how to indent the line.
1103 INDENT-POINT is the position where the user typed TAB, or equivalent.
1104 Point is located at the point to indent under (for default indentation);
1105 STATE is the `parse-partial-sexp' state for that position.
1107 If the current line is in a call to a Lisp function
1108 which has a non-nil property `lisp-indent-function',
1109 that specifies how to do the indentation. The property value can be
1110 * `defun', meaning indent `defun'-style;
1111 * an integer N, meaning indent the first N arguments specially
1112 like ordinary function arguments and then indent any further
1113 arguments like a body;
1114 * a function to call just as this function was called.
1115 If that function returns nil, that means it doesn't specify
1118 This function also returns nil meaning don't specify the indentation."
1119 (let ((normal-indent (current-column)))
1120 (goto-char (1+ (elt state 1)))
1121 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1122 (if (and (elt state 2)
1123 (not (looking-at "\\sw\\|\\s_")))
1124 ;; car of form doesn't seem to be a symbol
1126 (if (not (> (save-excursion (forward-line 1) (point))
1127 calculate-lisp-indent-last-sexp))
1128 (progn (goto-char calculate-lisp-indent-last-sexp)
1130 (parse-partial-sexp (point)
1131 calculate-lisp-indent-last-sexp 0 t)))
1132 ;; Indent under the list or under the first sexp on the same
1133 ;; line as calculate-lisp-indent-last-sexp. Note that first
1134 ;; thing on that line has to be complete sexp since we are
1135 ;; inside the innermost containing sexp.
1136 (backward-prefix-chars)
1138 (let ((function (buffer-substring (point)
1139 (progn (forward-sexp 1) (point))))
1141 (setq method (or (get (intern-soft function) 'lisp-indent-function)
1142 (get (intern-soft function) 'lisp-indent-hook)))
1143 (cond ((or (eq method 'defun)
1145 (> (length function) 3)
1146 (string-match "\\`def" function)))
1147 (lisp-indent-defform state indent-point))
1149 (lisp-indent-specform method state
1150 indent-point normal-indent))
1152 (funcall method indent-point state)))))))
1154 (defcustom lisp-body-indent 2
1155 "Number of columns to indent the second line of a `(def...)' form."
1158 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1160 (defun lisp-indent-specform (count state indent-point normal-indent)
1161 (let ((containing-form-start (elt state 1))
1163 body-indent containing-form-column)
1164 ;; Move to the start of containing form, calculate indentation
1165 ;; to use for non-distinguished forms (> count), and move past the
1166 ;; function symbol. lisp-indent-function guarantees that there is at
1167 ;; least one word or symbol character following open paren of containing
1169 (goto-char containing-form-start)
1170 (setq containing-form-column (current-column))
1171 (setq body-indent (+ lisp-body-indent containing-form-column))
1174 ;; Now find the start of the last form.
1175 (parse-partial-sexp (point) indent-point 1 t)
1176 (while (and (< (point) indent-point)
1179 (setq count (1- count))
1181 (parse-partial-sexp (point) indent-point 1 t))
1183 ;; Point is sitting on first character of last (or count) sexp.
1185 ;; A distinguished form. If it is the first or second form use double
1186 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1187 ;; to 2 (the default), this just happens to work the same with if as
1188 ;; the older code, but it makes unwind-protect, condition-case,
1189 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1190 ;; less hacked, behavior can be obtained by replacing below with
1191 ;; (list normal-indent containing-form-start).
1192 (if (<= (- i count) 1)
1193 (list (+ containing-form-column (* 2 lisp-body-indent))
1194 containing-form-start)
1195 (list normal-indent containing-form-start))
1196 ;; A non-distinguished form. Use body-indent if there are no
1197 ;; distinguished forms and this is the first undistinguished form,
1198 ;; or if this is the first undistinguished form and the preceding
1199 ;; distinguished form has indentation at least as great as body-indent.
1200 (if (or (and (= i 0) (= count 0))
1201 (and (= count 0) (<= body-indent normal-indent)))
1205 (defun lisp-indent-defform (state indent-point)
1206 (goto-char (car (cdr state)))
1208 (if (> (point) (car (cdr (cdr state))))
1210 (goto-char (car (cdr state)))
1211 (+ lisp-body-indent (current-column)))))
1214 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1215 ;; like defun if the first form is placed on the next line, otherwise
1216 ;; it is indented like any other form (i.e. forms line up under first).
1218 (put 'lambda 'lisp-indent-function 'defun)
1219 (put 'autoload 'lisp-indent-function 'defun)
1220 (put 'progn 'lisp-indent-function 0)
1221 (put 'prog1 'lisp-indent-function 1)
1222 (put 'prog2 'lisp-indent-function 2)
1223 (put 'save-excursion 'lisp-indent-function 0)
1224 (put 'save-window-excursion 'lisp-indent-function 0)
1225 (put 'save-selected-window 'lisp-indent-function 0)
1226 (put 'save-restriction 'lisp-indent-function 0)
1227 (put 'save-match-data 'lisp-indent-function 0)
1228 (put 'save-current-buffer 'lisp-indent-function 0)
1229 (put 'with-current-buffer 'lisp-indent-function 1)
1230 (put 'combine-after-change-calls 'lisp-indent-function 0)
1231 (put 'with-output-to-string 'lisp-indent-function 0)
1232 (put 'with-temp-file 'lisp-indent-function 1)
1233 (put 'with-temp-buffer 'lisp-indent-function 0)
1234 (put 'with-temp-message 'lisp-indent-function 1)
1235 (put 'with-syntax-table 'lisp-indent-function 1)
1236 (put 'let 'lisp-indent-function 1)
1237 (put 'let* 'lisp-indent-function 1)
1238 (put 'while 'lisp-indent-function 1)
1239 (put 'if 'lisp-indent-function 2)
1240 (put 'read-if 'lisp-indent-function 2)
1241 (put 'catch 'lisp-indent-function 1)
1242 (put 'condition-case 'lisp-indent-function 2)
1243 (put 'unwind-protect 'lisp-indent-function 1)
1244 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1245 (put 'eval-after-load 'lisp-indent-function 1)
1246 (put 'dolist 'lisp-indent-function 1)
1247 (put 'dotimes 'lisp-indent-function 1)
1248 (put 'when 'lisp-indent-function 1)
1249 (put 'unless 'lisp-indent-function 1)
1251 (defun indent-sexp (&optional endpos)
1252 "Indent each line of the list starting just after point.
1253 If optional arg ENDPOS is given, indent each line, stopping when
1254 ENDPOS is encountered."
1256 (let ((indent-stack (list nil))
1258 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1259 ;; so that calculate-lisp-indent will find the beginning of
1260 ;; the defun we are in.
1261 ;; If ENDPOS is nil, it is safe not to scan before point
1262 ;; since every line we indent is more deeply nested than point is.
1263 (starting-point (if endpos nil (point)))
1264 (last-point (point))
1265 last-depth bol outer-loop-done inner-loop-done state this-indent)
1267 ;; Get error now if we don't have a complete sexp after point.
1268 (save-excursion (forward-sexp 1)))
1270 (setq outer-loop-done nil)
1271 (while (if endpos (< (point) endpos)
1272 (not outer-loop-done))
1273 (setq last-depth next-depth
1274 inner-loop-done nil)
1275 ;; Parse this line so we can learn the state
1276 ;; to indent the next line.
1277 ;; This inner loop goes through only once
1278 ;; unless a line ends inside a string.
1279 (while (and (not inner-loop-done)
1280 (not (setq outer-loop-done (eobp))))
1281 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1283 (setq next-depth (car state))
1284 ;; If the line contains a comment other than the sort
1285 ;; that is indented like code,
1286 ;; indent it now with indent-for-comment.
1287 ;; Comments indented like code are right already.
1288 ;; In any case clear the in-comment flag in the state
1289 ;; because parse-partial-sexp never sees the newlines.
1290 (if (car (nthcdr 4 state))
1291 (progn (indent-for-comment)
1293 (setcar (nthcdr 4 state) nil)))
1294 ;; If this line ends inside a string,
1295 ;; go straight to next line, remaining within the inner loop,
1296 ;; and turn off the \-flag.
1297 (if (car (nthcdr 3 state))
1300 (setcar (nthcdr 5 state) nil))
1301 (setq inner-loop-done t)))
1305 (setq indent-stack (nconc indent-stack
1306 (make-list (- next-depth) nil))
1307 last-depth (- last-depth next-depth)
1310 ;; Decide whether to exit.
1312 ;; If we have already reached the specified end,
1313 ;; give up and do not reindent this line.
1314 (if (<= endpos (point))
1315 (setq outer-loop-done t))
1316 ;; If no specified end, we are done if we have finished one sexp.
1317 (if (<= next-depth 0)
1318 (setq outer-loop-done t)))
1319 (unless outer-loop-done
1320 (while (> last-depth next-depth)
1321 (setq indent-stack (cdr indent-stack)
1322 last-depth (1- last-depth)))
1323 (while (< last-depth next-depth)
1324 (setq indent-stack (cons nil indent-stack)
1325 last-depth (1+ last-depth)))
1326 ;; Now indent the next line according
1327 ;; to what we learned from parsing the previous one.
1329 (skip-chars-forward " \t")
1330 ;; But not if the line is blank, or just a comment
1331 ;; (except for double-semi comments; indent them as usual).
1332 (if (or (eobp) (looking-at "\\s<\\|\n"))
1334 (if (and (car indent-stack)
1335 (>= (car indent-stack) 0))
1336 (setq this-indent (car indent-stack))
1337 (let ((val (calculate-lisp-indent
1338 (if (car indent-stack) (- (car indent-stack))
1341 (setq this-indent val)
1343 (setcar indent-stack
1344 (setq this-indent val))
1345 (setcar indent-stack (- (car (cdr val))))
1346 (setq this-indent (car val))))))
1347 (if (and this-indent (/= (current-column) this-indent))
1348 (progn (delete-region bol (point))
1349 (indent-to this-indent)))))
1351 (setq outer-loop-done (= (point) last-point))
1352 (setq last-point (point)))))))
1354 (defun indent-pp-sexp (&optional arg)
1355 "Indent each line of the list starting just after point, or prettyprint it.
1356 A prefix argument specifies pretty-printing."
1361 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1363 (goto-char (point-max))
1364 (if (eq (char-before) ?\n)
1365 (delete-char -1)))))
1368 ;;;; Lisp paragraph filling commands.
1370 (defcustom emacs-lisp-docstring-fill-column 65
1371 "Value of `fill-column' to use when filling a docstring.
1372 Any non-integer value means do not use a different value of
1373 `fill-column' when filling docstrings."
1374 :type '(choice (integer)
1375 (const :tag "Use the current `fill-column'" t))
1378 (defun lisp-fill-paragraph (&optional justify)
1379 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1380 If any of the current line is a comment, fill the comment or the
1381 paragraph of it that point is in, preserving the comment's indentation
1382 and initial semicolons."
1384 (or (fill-comment-paragraph justify)
1385 ;; Since fill-comment-paragraph returned nil, that means we're not in
1386 ;; a comment: Point is on a program line; we are interested
1387 ;; particularly in docstring lines.
1389 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1390 ;; are buffer-local, but we avoid changing them so that they can be set
1391 ;; to make `forward-paragraph' and friends do something the user wants.
1393 ;; `paragraph-start': The `(' in the character alternative and the
1394 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1395 ;; sexps and backquoted sexps that follow a docstring from being filled
1396 ;; with the docstring. This setting has the consequence of inhibiting
1397 ;; filling many program lines that are not docstrings, which is sensible,
1398 ;; because the user probably asked to fill program lines by accident, or
1399 ;; expecting indentation (perhaps we should try to do indenting in that
1400 ;; case). The `;' and `:' stop the paragraph being filled at following
1401 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1402 ;; escaped to keep font-locking, filling, & paren matching in the source
1405 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1406 ;; a docstring and identifies it as a paragraph separator, so that it
1407 ;; won't be filled. (Since the first line of documentation stands alone
1408 ;; in some contexts, filling should not alter the contents the author has
1409 ;; chosen.) Only the first line of a docstring begins with whitespace
1410 ;; and a quotation mark and ends with a period or (rarely) a comma.
1412 ;; The `fill-column' is temporarily bound to
1413 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1414 (let ((paragraph-start (concat paragraph-start
1415 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1417 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1418 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1419 (derived-mode-p 'emacs-lisp-mode))
1420 emacs-lisp-docstring-fill-column
1422 (fill-paragraph justify))
1423 ;; Never return nil.
1426 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1427 "Indent all lines of code, starting in the region, sideways by ARG columns.
1428 Does not affect lines starting inside comments or strings, assuming that
1429 the start of the region is not inside them.
1431 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1432 The last is a regexp which, if matched at the beginning of a line,
1433 means don't indent that line."
1434 (interactive "r\np")
1438 (setq end (point-marker))
1441 (setq state (parse-partial-sexp (point)
1443 (forward-line 1) (point))
1445 (while (< (point) end)
1446 (or (car (nthcdr 3 state))
1447 (and nochange-regexp
1448 (looking-at nochange-regexp))
1449 ;; If line does not start in string, indent it
1450 (let ((indent (current-indentation)))
1451 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1453 (indent-to (max 0 (+ indent arg)) 0))))
1454 (setq state (parse-partial-sexp (point)
1456 (forward-line 1) (point))
1459 (provide 'lisp-mode)
1461 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1462 ;;; lisp-mode.el ends here