1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
7 ;; Keywords: lisp, languages
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
29 ;; This mode is documented in the Emacs manual.
33 (defvar font-lock-comment-face)
34 (defvar font-lock-doc-face)
35 (defvar font-lock-keywords-case-fold-search)
36 (defvar font-lock-string-face)
38 (defvar lisp-mode-abbrev-table nil)
40 (define-abbrev-table 'lisp-mode-abbrev-table ())
42 (defvar emacs-lisp-mode-syntax-table
43 (let ((table (make-syntax-table)))
46 (modify-syntax-entry i "_ " table)
50 (modify-syntax-entry i "_ " table)
54 (modify-syntax-entry i "_ " table)
58 (modify-syntax-entry i "_ " table)
60 (modify-syntax-entry ?\s " " table)
61 ;; Non-break space acts as whitespace.
62 (modify-syntax-entry ?\x8a0 " " table)
63 (modify-syntax-entry ?\t " " table)
64 (modify-syntax-entry ?\f " " table)
65 (modify-syntax-entry ?\n "> " table)
66 ;; This is probably obsolete since nowadays such features use overlays.
67 ;; ;; Give CR the same syntax as newline, for selective-display.
68 ;; (modify-syntax-entry ?\^m "> " table)
69 (modify-syntax-entry ?\; "< " table)
70 (modify-syntax-entry ?` "' " table)
71 (modify-syntax-entry ?' "' " table)
72 (modify-syntax-entry ?, "' " table)
73 (modify-syntax-entry ?@ "' " table)
74 ;; Used to be singlequote; changed for flonums.
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)
81 (modify-syntax-entry ?\[ "(] " table)
82 (modify-syntax-entry ?\] ")[ " table))
85 (defvar lisp-mode-syntax-table
86 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
87 (modify-syntax-entry ?\[ "_ " table)
88 (modify-syntax-entry ?\] "_ " table)
89 (modify-syntax-entry ?# "' 14b" table)
90 (modify-syntax-entry ?| "\" 23bn" table)
93 (defvar lisp-imenu-generic-expression
96 (purecopy (concat "^\\s-*("
99 '("defun" "defun*" "defsubst" "defmacro"
100 "defadvice" "define-skeleton"
101 "define-minor-mode" "define-global-minor-mode"
102 "define-globalized-minor-mode"
103 "define-derived-mode" "define-generic-mode"
104 "define-compiler-macro" "define-modify-macro"
105 "defsetf" "define-setf-expander"
106 "define-method-combination"
107 "defgeneric" "defmethod") t))
108 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
110 (list (purecopy "Variables")
111 (purecopy (concat "^\\s-*("
114 '("defvar" "defconst" "defconstant" "defcustom"
115 "defparameter" "define-symbol-macro") t))
116 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
118 (list (purecopy "Types")
119 (purecopy (concat "^\\s-*("
122 '("defgroup" "deftheme" "deftype" "defstruct"
123 "defclass" "define-condition" "define-widget"
124 "defface" "defpackage") t))
125 "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
128 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
130 ;; This was originally in autoload.el and is still used there.
131 (put 'autoload 'doc-string-elt 3)
132 (put 'defun 'doc-string-elt 3)
133 (put 'defun* 'doc-string-elt 3)
134 (put 'defvar 'doc-string-elt 3)
135 (put 'defcustom 'doc-string-elt 3)
136 (put 'deftheme 'doc-string-elt 2)
137 (put 'defconst 'doc-string-elt 3)
138 (put 'defmacro 'doc-string-elt 3)
139 (put 'defmacro* 'doc-string-elt 3)
140 (put 'defsubst 'doc-string-elt 3)
141 (put 'defstruct 'doc-string-elt 2)
142 (put 'define-skeleton 'doc-string-elt 2)
143 (put 'define-derived-mode 'doc-string-elt 4)
144 (put 'define-compilation-mode 'doc-string-elt 3)
145 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
146 (put 'define-minor-mode 'doc-string-elt 2)
147 (put 'easy-mmode-define-global-mode 'doc-string-elt 2)
148 (put 'define-global-minor-mode 'doc-string-elt 2)
149 (put 'define-globalized-minor-mode 'doc-string-elt 2)
150 (put 'define-generic-mode 'doc-string-elt 7)
151 (put 'define-ibuffer-filter 'doc-string-elt 2)
152 (put 'define-ibuffer-op 'doc-string-elt 3)
153 (put 'define-ibuffer-sorter 'doc-string-elt 2)
154 (put 'lambda 'doc-string-elt 2)
155 (put 'defalias 'doc-string-elt 3)
156 (put 'defvaralias 'doc-string-elt 3)
157 (put 'define-category 'doc-string-elt 2)
159 (defvar lisp-doc-string-elt-property 'doc-string-elt
160 "The symbol property that holds the docstring position info.")
162 (defun lisp-font-lock-syntactic-face-function (state)
164 ;; This might be a (doc)string or a |...| symbol.
165 (let ((startpos (nth 8 state)))
166 (if (eq (char-after startpos) ?|)
167 ;; This is not a string, but a |...| symbol.
169 (let* ((listbeg (nth 1 state))
170 (firstsym (and listbeg
173 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
175 (docelt (and firstsym (get (intern-soft firstsym)
176 lisp-doc-string-elt-property))))
178 ;; It's a string in a form that can have a docstring.
179 ;; Check whether it's in docstring position.
181 (when (functionp docelt)
182 (goto-char (match-end 1))
183 (setq docelt (funcall docelt)))
187 (while (and (> docelt 0) (< (point) startpos)
188 (progn (forward-sexp 1) t))
189 (setq docelt (1- docelt)))
191 (and (zerop docelt) (<= (point) startpos)
192 (progn (forward-comment (point-max)) t)
193 (= (point) (nth 8 state)))))
195 font-lock-string-face))))
196 font-lock-comment-face))
198 ;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is
199 ;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el
200 (defun lisp-mode-variables (&optional lisp-syntax)
202 (set-syntax-table lisp-mode-syntax-table))
203 (setq local-abbrev-table lisp-mode-abbrev-table)
204 (make-local-variable 'paragraph-ignore-fill-prefix)
205 (setq paragraph-ignore-fill-prefix t)
206 (make-local-variable 'fill-paragraph-function)
207 (setq fill-paragraph-function 'lisp-fill-paragraph)
208 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
209 ;; a single docstring. Let's fix it here.
210 (set (make-local-variable 'adaptive-fill-function)
211 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
212 ;; Adaptive fill mode gets in the way of auto-fill,
213 ;; and should make no difference for explicit fill
214 ;; because lisp-fill-paragraph should do the job.
215 ;; I believe that newcomment's auto-fill code properly deals with it -stef
216 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
217 (make-local-variable 'indent-line-function)
218 (setq indent-line-function 'lisp-indent-line)
219 (make-local-variable 'parse-sexp-ignore-comments)
220 (setq parse-sexp-ignore-comments t)
221 (make-local-variable 'outline-regexp)
222 (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
223 (make-local-variable 'outline-level)
224 (setq outline-level 'lisp-outline-level)
225 (make-local-variable 'comment-start)
226 (setq comment-start ";")
227 (make-local-variable 'comment-start-skip)
228 ;; Look within the line for a ; following an even number of backslashes
229 ;; after either a non-backslash or the line beginning.
230 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
231 (make-local-variable 'font-lock-comment-start-skip)
232 ;; Font lock mode uses this only when it KNOWS a comment is starting.
233 (setq font-lock-comment-start-skip ";+ *")
234 (make-local-variable 'comment-add)
235 (setq comment-add 1) ;default to `;;' in comment-region
236 (make-local-variable 'comment-column)
237 (setq comment-column 40)
238 ;; Don't get confused by `;' in doc strings when paragraph-filling.
239 (set (make-local-variable 'comment-use-global-state) t)
240 (make-local-variable 'imenu-generic-expression)
241 (setq imenu-generic-expression lisp-imenu-generic-expression)
242 (make-local-variable 'multibyte-syntax-as-symbol)
243 (setq multibyte-syntax-as-symbol t)
244 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
245 (setq font-lock-defaults
246 '((lisp-font-lock-keywords
247 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
248 nil nil (("+-*/.<>=!?$%_&~^:@" . "w")) nil
249 (font-lock-mark-block-function . mark-defun)
250 (font-lock-syntactic-face-function
251 . lisp-font-lock-syntactic-face-function))))
253 (defun lisp-outline-level ()
254 "Lisp mode `outline-level' function."
255 (let ((len (- (match-end 0) (match-beginning 0))))
256 (if (looking-at "(\\|;;;###autoload")
260 (defvar lisp-mode-shared-map
261 (let ((map (make-sparse-keymap)))
262 (define-key map "\e\C-q" 'indent-sexp)
263 (define-key map "\177" 'backward-delete-char-untabify)
264 ;; This gets in the way when viewing a Lisp file in view-mode. As
265 ;; long as [backspace] is mapped into DEL via the
266 ;; function-key-map, this should remain disabled!!
267 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
269 "Keymap for commands shared by all sorts of Lisp modes.")
271 (defvar emacs-lisp-mode-map
272 (let ((map (make-sparse-keymap "Emacs-Lisp"))
273 (menu-map (make-sparse-keymap "Emacs-Lisp"))
274 (prof-map (make-sparse-keymap))
275 (tracing-map (make-sparse-keymap)))
276 (set-keymap-parent map lisp-mode-shared-map)
277 (define-key map "\e\t" 'lisp-complete-symbol)
278 (define-key map "\e\C-x" 'eval-defun)
279 (define-key map "\e\C-q" 'indent-pp-sexp)
280 (define-key map [menu-bar emacs-lisp] (cons "Emacs-Lisp" menu-map))
281 (define-key menu-map [eldoc]
282 '(menu-item "Auto-Display Documentation Strings" eldoc-mode
283 :button (:toggle . (bound-and-true-p eldoc-mode))
284 :help "Display the documentation string for the item under cursor"))
285 (define-key menu-map [checkdoc]
286 '(menu-item "Check Documentation Strings" checkdoc
287 :help "Check documentation strings for style requirements"))
288 (define-key menu-map [re-builder]
289 '(menu-item "Construct Regexp" re-builder
290 :help "Construct a regexp interactively"))
291 (define-key menu-map [tracing] (cons "Tracing" tracing-map))
292 (define-key tracing-map [tr-a]
293 '(menu-item "Untrace all" untrace-all
294 :help "Untraces all currently traced functions"))
295 (define-key tracing-map [tr-uf]
296 '(menu-item "Untrace function..." untrace-function
297 :help "Untraces FUNCTION and possibly activates all remaining advice"))
298 (define-key tracing-map [tr-sep] '("--"))
299 (define-key tracing-map [tr-q]
300 '(menu-item "Trace function quietly..." trace-function-background
301 :help "Trace the function with trace output going quietly to a buffer"))
302 (define-key tracing-map [tr-f]
303 '(menu-item "Trace function..." trace-function
304 :help "Trace the function given as a argument"))
305 (define-key menu-map [profiling] (cons "Profiling" prof-map))
306 (define-key prof-map [prof-restall]
307 '(menu-item "Remove Instrumentation for All Functions" elp-restore-all
308 :help "Restore the original definitions of all functions being profiled"))
309 (define-key prof-map [prof-restfunc]
310 '(menu-item "Remove Instrumentation for Function..." elp-restore-function
311 :help "Restore an instrumented function to its original definition"))
313 (define-key prof-map [sep-rem] '("--"))
314 (define-key prof-map [prof-resall]
315 '(menu-item "Reset Counters for All Functions" elp-reset-all
316 :help "Reset the profiling information for all functions being profiled"))
317 (define-key prof-map [prof-resfunc]
318 '(menu-item "Reset Counters for Function..." elp-reset-function
319 :help "Reset the profiling information for a function"))
320 (define-key prof-map [prof-res]
321 '(menu-item "Show Profiling Results" elp-results
322 :help "Display current profiling results"))
323 (define-key prof-map [prof-pack]
324 '(menu-item "Instrument Package..." elp-instrument-package
325 :help "Instrument for profiling all function that start with a prefix"))
326 (define-key prof-map [prof-func]
327 '(menu-item "Instrument Function..." elp-instrument-function
328 :help "Instrument a function for profiling"))
329 (define-key menu-map [edebug-defun]
330 '(menu-item "Instrument Function for Debugging" edebug-defun
331 :help "Evaluate the top level form point is in, stepping through with Edebug"
333 (define-key menu-map [separator-byte] '("--"))
334 (define-key menu-map [disas]
335 '(menu-item "Disassemble byte compiled object..." disassemble
336 :help "Print disassembled code for OBJECT in a buffer"))
337 (define-key menu-map [byte-recompile]
338 '(menu-item "Byte-recompile Directory..." byte-recompile-directory
339 :help "Recompile every `.el' file in DIRECTORY that needs recompilation"))
340 (define-key menu-map [emacs-byte-compile-and-load]
341 '(menu-item "Byte-compile And Load" emacs-lisp-byte-compile-and-load
342 :help "Byte-compile the current file (if it has changed), then load compiled code"))
343 (define-key menu-map [byte-compile]
344 '(menu-item "Byte-compile This File" emacs-lisp-byte-compile
345 :help "Byte compile the file containing the current buffer"))
346 (define-key menu-map [separator-eval] '("--"))
347 (define-key menu-map [ielm]
348 '(menu-item "Interactive Expression Evaluation" ielm
349 :help "Interactively evaluate Emacs Lisp expressions"))
350 (define-key menu-map [eval-buffer]
351 '(menu-item "Evaluate Buffer" eval-buffer
352 :help "Execute the current buffer as Lisp code"))
353 (define-key menu-map [eval-region]
354 '(menu-item "Evaluate Region" eval-region
355 :help "Execute the region as Lisp code"
356 :enable mark-active))
357 (define-key menu-map [eval-sexp]
358 '(menu-item "Evaluate Last S-expression" eval-last-sexp
359 :help "Evaluate sexp before point; print value in minibuffer"))
360 (define-key menu-map [separator-format] '("--"))
361 (define-key menu-map [comment-region]
362 '(menu-item "Comment Out Region" comment-region
363 :help "Comment or uncomment each line in the region"
364 :enable mark-active))
365 (define-key menu-map [indent-region]
366 '(menu-item "Indent Region" indent-region
367 :help "Indent each nonblank line in the region"
368 :enable mark-active))
369 (define-key menu-map [indent-line] '("Indent Line" . lisp-indent-line))
371 "Keymap for Emacs Lisp mode.
372 All commands in `lisp-mode-shared-map' are inherited by this map.")
374 (defun emacs-lisp-byte-compile ()
375 "Byte compile the file containing the current buffer."
378 (byte-compile-file buffer-file-name)
379 (error "The buffer must be saved in a file first")))
381 (defun emacs-lisp-byte-compile-and-load ()
382 "Byte-compile the current file (if it has changed), then load compiled code."
385 (error "The buffer must be saved in a file first"))
387 ;; Recompile if file or buffer has changed since last compilation.
388 (if (and (buffer-modified-p)
389 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
391 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name)))
392 (if (file-newer-than-file-p compiled-file-name buffer-file-name)
393 (load-file compiled-file-name)
394 (byte-compile-file buffer-file-name t))))
396 (defcustom emacs-lisp-mode-hook nil
397 "Hook run when entering Emacs Lisp mode."
398 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
402 (defcustom lisp-mode-hook nil
403 "Hook run when entering Lisp mode."
404 :options '(imenu-add-menubar-index)
408 (defcustom lisp-interaction-mode-hook nil
409 "Hook run when entering Lisp Interaction mode."
410 :options '(turn-on-eldoc-mode)
414 (defun emacs-lisp-mode ()
415 "Major mode for editing Lisp code to run in Emacs.
417 Delete converts tabs to spaces as it moves back.
418 Blank lines separate paragraphs. Semicolons start comments.
419 \\{emacs-lisp-mode-map}
420 Entry to this mode calls the value of `emacs-lisp-mode-hook'
421 if that value is non-nil."
423 (kill-all-local-variables)
424 (use-local-map emacs-lisp-mode-map)
425 (set-syntax-table emacs-lisp-mode-syntax-table)
426 (setq major-mode 'emacs-lisp-mode)
427 (setq mode-name "Emacs-Lisp")
428 (lisp-mode-variables)
429 (setq imenu-case-fold-search nil)
430 (run-mode-hooks 'emacs-lisp-mode-hook))
431 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
433 (defvar lisp-mode-map
434 (let ((map (make-sparse-keymap))
435 (menu-map (make-sparse-keymap "Lisp")))
436 (set-keymap-parent map lisp-mode-shared-map)
437 (define-key map "\e\C-x" 'lisp-eval-defun)
438 (define-key map "\C-c\C-z" 'run-lisp)
439 (define-key map [menu-bar lisp] (cons "Lisp" menu-map))
440 (define-key menu-map [run-lisp]
441 '(menu-item "Run inferior Lisp" run-lisp
442 :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))
443 (define-key menu-map [ev-def]
444 '(menu-item "Eval defun" lisp-eval-defun
445 :help "Send the current defun to the Lisp process made by M-x run-lisp"))
446 (define-key menu-map [ind-sexp]
447 '(menu-item "Indent sexp" indent-sexp
448 :help "Indent each line of the list starting just after point"))
450 "Keymap for ordinary Lisp mode.
451 All commands in `lisp-mode-shared-map' are inherited by this map.")
454 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
456 Delete converts tabs to spaces as it moves back.
457 Blank lines separate paragraphs. Semicolons start comments.
459 Note that `run-lisp' may be used either to start an inferior Lisp job
460 or to switch back to an existing one.
462 Entry to this mode calls the value of `lisp-mode-hook'
463 if that value is non-nil."
465 (kill-all-local-variables)
466 (use-local-map lisp-mode-map)
467 (setq major-mode 'lisp-mode)
468 (setq mode-name "Lisp")
469 (lisp-mode-variables)
470 (make-local-variable 'comment-start-skip)
471 (setq comment-start-skip
472 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
473 (make-local-variable 'font-lock-keywords-case-fold-search)
474 (setq font-lock-keywords-case-fold-search t)
475 (setq imenu-case-fold-search t)
476 (set-syntax-table lisp-mode-syntax-table)
477 (run-mode-hooks 'lisp-mode-hook))
478 (put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default)
480 (defun lisp-find-tag-default ()
481 (let ((default (find-tag-default)))
482 (when (stringp default)
483 (if (string-match ":+" default)
484 (substring default (match-end 0))
487 ;; Used in old LispM code.
488 (defalias 'common-lisp-mode 'lisp-mode)
490 ;; This will do unless inf-lisp.el is loaded.
491 (defun lisp-eval-defun (&optional and-go)
492 "Send the current defun to the Lisp process made by \\[run-lisp]."
494 (error "Process lisp does not exist"))
496 (defvar lisp-interaction-mode-map
497 (let ((map (make-sparse-keymap))
498 (menu-map (make-sparse-keymap "Lisp-Interaction")))
499 (set-keymap-parent map lisp-mode-shared-map)
500 (define-key map "\e\C-x" 'eval-defun)
501 (define-key map "\e\C-q" 'indent-pp-sexp)
502 (define-key map "\e\t" 'lisp-complete-symbol)
503 (define-key map "\n" 'eval-print-last-sexp)
504 (define-key map [menu-bar lisp-interaction] (cons "Lisp-Interaction" menu-map))
505 (define-key menu-map [eval-defun]
506 '(menu-item "Evaluate Defun" eval-defun
507 :help "Evaluate the top-level form containing point, or after point"))
508 (define-key menu-map [eval-print-last-sexp]
509 '(menu-item "Evaluate and print" eval-print-last-sexp
510 :help "Evaluate sexp before point; print value into current buffer"))
511 (define-key menu-map [edebug-defun-lisp-interaction]
512 '(menu-item "Instrument Function for Debugging" edebug-defun
513 :help "Evaluate the top level form point is in, stepping through with Edebug"
515 (define-key menu-map [indent-pp-sexp]
516 '(menu-item "Indent or Pretty-Print" indent-pp-sexp
517 :help "Indent each line of the list starting just after point, or prettyprint it"))
518 (define-key menu-map [lisp-complete-symbol]
519 '(menu-item "Complete Lisp Symbol" lisp-complete-symbol
520 :help "Perform completion on Lisp symbol preceding point"))
522 "Keymap for Lisp Interaction mode.
523 All commands in `lisp-mode-shared-map' are inherited by this map.")
525 (defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table)
526 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
527 "Major mode for typing and evaluating Lisp forms.
528 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
529 before point, and prints its value into the buffer, advancing point.
530 Note that printing is controlled by `eval-expression-print-length'
531 and `eval-expression-print-level'.
534 Delete converts tabs to spaces as it moves back.
535 Paragraphs are separated only by blank lines.
536 Semicolons start comments.
537 \\{lisp-interaction-mode-map}
538 Entry to this mode calls the value of `lisp-interaction-mode-hook'
539 if that value is non-nil.")
541 (defun eval-print-last-sexp ()
542 "Evaluate sexp before point; print value into current buffer.
544 If `eval-expression-debug-on-error' is non-nil, which is the default,
545 this command arranges for all errors to enter the debugger.
547 Note that printing the result is controlled by the variables
548 `eval-expression-print-length' and `eval-expression-print-level',
551 (let ((standard-output (current-buffer)))
557 (defun last-sexp-setup-props (beg end value alt1 alt2)
558 "Set up text properties for the output of `eval-last-sexp-1'.
559 BEG and END are the start and end of the output in current-buffer.
560 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
561 alternative printed representations that can be displayed."
562 (let ((map (make-sparse-keymap)))
563 (define-key map "\C-m" 'last-sexp-toggle-display)
564 (define-key map [down-mouse-2] 'mouse-set-point)
565 (define-key map [mouse-2] 'last-sexp-toggle-display)
568 `(printed-value (,value ,alt1 ,alt2)
571 help-echo "RET, mouse-2: toggle abbreviated display"
572 rear-nonsticky (mouse-face keymap help-echo
576 (defun last-sexp-toggle-display (&optional arg)
577 "Toggle between abbreviated and unabbreviated printed representations."
581 (let ((value (get-text-property (point) 'printed-value)))
583 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
586 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
587 (standard-output (current-buffer))
589 (delete-region beg end)
590 (insert (nth 1 value))
592 (setq point (1- (point))))
593 (last-sexp-setup-props beg (point)
597 (goto-char (min (point-max) point)))))))
599 (defun prin1-char (char)
600 "Return a string representing CHAR as a character rather than as an integer.
601 If CHAR is not a character, return nil."
604 (let ((c (event-basic-type char))
605 (mods (event-modifiers char))
607 ;; Prevent ?A from turning into ?\S-a.
608 (if (and (memq 'shift mods)
609 (zerop (logand char ?\S-\^@))
610 (not (let ((case-fold-search nil))
611 (char-equal c (upcase c)))))
612 (setq c (upcase c) mods nil))
613 ;; What string are we considering using?
620 (cond ((eq modif 'super) "\\s-")
621 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
624 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
629 ;; Verify the string reads a CHAR, not to some other character.
630 ;; If it doesn't, return nil instead.
632 (= (car (read-from-string string)) char)
636 (defun preceding-sexp ()
637 "Return sexp before the point."
638 (let ((opoint (point))
642 (with-syntax-table emacs-lisp-mode-syntax-table
643 ;; If this sexp appears to be enclosed in `...'
644 ;; then ignore the surrounding quotes.
646 (or (eq (following-char) ?\')
647 (eq (preceding-char) ?\')))
649 ;; If we were after `?\e' (or similar case),
650 ;; use the whole thing, not just the `e'.
651 (when (eq (preceding-char) ?\\)
653 (when (eq (preceding-char) ??)
657 (when (eq (preceding-char) ?=)
660 (skip-chars-backward "0-9#=")
661 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
666 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
667 ;; `variable' so that the value is returned, not the
669 (if (and ignore-quotes
670 (eq (following-char) ?`))
672 (narrow-to-region (point-min) opoint)
673 (setq expr (read (current-buffer)))
674 ;; If it's an (interactive ...) form, it's more
675 ;; useful to show how an interactive call would
678 (eq (car expr) 'interactive)
680 (list 'call-interactively
689 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
690 "Evaluate sexp before point; print value in minibuffer.
691 With argument, print output into current buffer."
692 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
693 (eval-last-sexp-print-value (eval (preceding-sexp)))))
696 (defun eval-last-sexp-print-value (value)
697 (let ((unabbreviated (let ((print-length nil) (print-level nil))
698 (prin1-to-string value)))
699 (print-length eval-expression-print-length)
700 (print-level eval-expression-print-level)
705 (let ((str (eval-expression-print-format value)))
706 (if str (princ str)))
708 (when (and (bufferp standard-output)
709 (or (not (null print-length))
710 (not (null print-level)))
711 (not (string= unabbreviated
712 (buffer-substring-no-properties beg end))))
713 (last-sexp-setup-props beg end value
715 (buffer-substring-no-properties beg end))
719 (defvar eval-last-sexp-fake-value (make-symbol "t"))
721 (defun eval-last-sexp (eval-last-sexp-arg-internal)
722 "Evaluate sexp before point; print value in minibuffer.
723 Interactively, with prefix argument, print output into current buffer.
725 If `eval-expression-debug-on-error' is non-nil, which is the default,
726 this command arranges for all errors to enter the debugger."
728 (if (null eval-expression-debug-on-error)
729 (eval-last-sexp-1 eval-last-sexp-arg-internal)
731 (let ((debug-on-error eval-last-sexp-fake-value))
732 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
734 (unless (eq (cdr value) eval-last-sexp-fake-value)
735 (setq debug-on-error (cdr value)))
738 (defun eval-defun-1 (form)
739 "Treat some expressions specially.
740 Reset the `defvar' and `defcustom' variables to the initial value.
741 Reinitialize the face according to the `defface' specification."
742 ;; The code in edebug-defun should be consistent with this, but not
743 ;; the same, since this gets a macroexpended form.
744 (cond ((not (listp form))
746 ((and (eq (car form) 'defvar)
747 (cdr-safe (cdr-safe form))
748 (boundp (cadr form)))
749 ;; Force variable to be re-set.
750 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
751 (setq-default ,(nth 1 form) ,(nth 2 form))))
752 ;; `defcustom' is now macroexpanded to
753 ;; `custom-declare-variable' with a quoted value arg.
754 ((and (eq (car form) 'custom-declare-variable)
755 (default-boundp (eval (nth 1 form))))
756 ;; Force variable to be bound.
757 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
759 ;; `defface' is macroexpanded to `custom-declare-face'.
760 ((eq (car form) 'custom-declare-face)
762 (setq face-new-frame-defaults
763 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
764 (put (eval (nth 1 form)) 'face-defface-spec nil)
765 ;; Setting `customized-face' to the new spec after calling
766 ;; the form, but preserving the old saved spec in `saved-face',
767 ;; imitates the situation when the new face spec is set
768 ;; temporarily for the current session in the customize
769 ;; buffer, thus allowing `face-user-default-spec' to use the
770 ;; new customized spec instead of the saved spec.
771 ;; Resetting `saved-face' temporarily to nil is needed to let
772 ;; `defface' change the spec, regardless of a saved spec.
774 (put ,(nth 1 form) 'saved-face
775 ',(get (eval (nth 1 form)) 'saved-face))
776 (put ,(nth 1 form) 'customized-face
778 (put (eval (nth 1 form)) 'saved-face nil)))
779 ((eq (car form) 'progn)
780 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
783 (defun eval-defun-2 ()
784 "Evaluate defun that point is in or before.
785 The value is displayed in the minibuffer.
786 If the current defun is actually a call to `defvar',
787 then reset the variable using the initial value expression
788 even if the variable already has some other value.
789 \(Normally `defvar' does not change the variable's value
790 if it already has a value.\)
792 With argument, insert value in current buffer after the defun.
793 Return the result of evaluation."
795 ;; FIXME: the print-length/level bindings should only be applied while
796 ;; printing, not while evaluating.
797 (let ((debug-on-error eval-expression-debug-on-error)
798 (print-length eval-expression-print-length)
799 (print-level eval-expression-print-level))
801 ;; Arrange for eval-region to "read" the (possibly) altered form.
802 ;; eval-region handles recording which file defines a function or
803 ;; variable. Re-written using `apply' to avoid capturing
804 ;; variables like `end'.
807 (let ((standard-output t)
809 ;; Read the form from the buffer, and record where it ends.
814 (setq form (read (current-buffer)))
816 ;; Alter the form if necessary.
817 (setq form (eval-defun-1 (macroexpand form)))
818 (list beg end standard-output
820 ;; Skipping to the end of the specified region
821 ;; will make eval-region return.
824 ;; The result of evaluation has been put onto VALUES. So return it.
827 (defun eval-defun (edebug-it)
828 "Evaluate the top-level form containing point, or after point.
830 If the current defun is actually a call to `defvar' or `defcustom',
831 evaluating it this way resets the variable using its initial value
832 expression even if the variable already has some other value.
833 \(Normally `defvar' and `defcustom' do not alter the value if there
834 already is one.) In an analogous way, evaluating a `defface'
835 overrides any customizations of the face, so that it becomes
836 defined exactly as the `defface' expression says.
838 If `eval-expression-debug-on-error' is non-nil, which is the default,
839 this command arranges for all errors to enter the debugger.
841 With a prefix argument, instrument the code for Edebug.
843 If acting on a `defun' for FUNCTION, and the function was
844 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
845 instrumented, just FUNCTION is printed.
847 If not acting on a `defun', the result of evaluation is displayed in
848 the minibuffer. This display is controlled by the variables
849 `eval-expression-print-length' and `eval-expression-print-level',
854 (eval-defun (not edebug-all-defs)))
856 (if (null eval-expression-debug-on-error)
858 (let ((old-value (make-symbol "t")) new-value value)
859 (let ((debug-on-error old-value))
860 (setq value (eval-defun-2))
861 (setq new-value debug-on-error))
862 (unless (eq old-value new-value)
863 (setq debug-on-error new-value))
866 ;; May still be used by some external Lisp-mode variant.
867 (define-obsolete-function-alias 'lisp-comment-indent
868 'comment-indent-default "22.1")
869 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
871 (defcustom lisp-indent-offset nil
872 "If non-nil, indent second line of expressions that many more columns."
874 :type '(choice nil integer))
875 (put 'lisp-body-indent 'safe-local-variable
876 (lambda (x) (or (null x) (integerp x))))
878 (defvar lisp-indent-function 'lisp-indent-function)
880 (defun lisp-indent-line (&optional whole-exp)
881 "Indent current line as Lisp code.
882 With argument, indent any additional lines of the same expression
883 rigidly along with this one."
885 (let ((indent (calculate-lisp-indent)) shift-amt end
886 (pos (- (point-max) (point)))
887 (beg (progn (beginning-of-line) (point))))
888 (skip-chars-forward " \t")
889 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
890 ;; Don't alter indentation of a ;;; comment line
891 ;; or a line that starts in a string.
892 (goto-char (- (point-max) pos))
893 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
894 ;; Single-semicolon comment lines should be indented
895 ;; as comment lines, not as code.
896 (progn (indent-for-comment) (forward-char -1))
897 (if (listp indent) (setq indent (car indent)))
898 (setq shift-amt (- indent (current-column)))
899 (if (zerop shift-amt)
901 (delete-region beg (point))
903 ;; If initial point was within line's indentation,
904 ;; position after the indentation. Else stay at same point in text.
905 (if (> (- (point-max) pos) (point))
906 (goto-char (- (point-max) pos)))
907 ;; If desired, shift remaining lines of expression the same amount.
908 (and whole-exp (not (zerop shift-amt))
917 (indent-code-rigidly beg end shift-amt)))))
919 (defvar calculate-lisp-indent-last-sexp)
921 (defun calculate-lisp-indent (&optional parse-start)
922 "Return appropriate indentation for current line as Lisp code.
923 In usual case returns an integer: the column to indent to.
924 If the value is nil, that means don't change the indentation
925 because the line starts inside a string.
927 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
928 This means that following lines at the same level of indentation
929 should not necessarily be indented the same as this line.
930 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
931 is the buffer position of the start of the containing expression."
934 (let ((indent-point (point))
936 ;; setting this to a number inhibits calling hook
939 calculate-lisp-indent-last-sexp containing-sexp)
941 (goto-char parse-start)
942 (beginning-of-defun))
943 ;; Find outermost containing sexp
944 (while (< (point) indent-point)
945 (setq state (parse-partial-sexp (point) indent-point 0)))
946 ;; Find innermost containing sexp
949 (> (setq paren-depth (elt state 0)) 0))
951 (setq calculate-lisp-indent-last-sexp (elt state 2))
952 (setq containing-sexp (elt state 1))
953 ;; Position following last unclosed open.
954 (goto-char (1+ containing-sexp))
955 ;; Is there a complete sexp since then?
956 (if (and calculate-lisp-indent-last-sexp
957 (> calculate-lisp-indent-last-sexp (point)))
958 ;; Yes, but is there a containing sexp after that?
959 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
961 (if (setq retry (car (cdr peek))) (setq state peek)))))
964 ;; Innermost containing sexp found
965 (goto-char (1+ containing-sexp))
966 (if (not calculate-lisp-indent-last-sexp)
967 ;; indent-point immediately follows open paren.
969 (setq desired-indent (current-column))
970 ;; Find the start of first element of containing sexp.
971 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
972 (cond ((looking-at "\\s(")
973 ;; First element of containing sexp is a list.
974 ;; Indent under that list.
976 ((> (save-excursion (forward-line 1) (point))
977 calculate-lisp-indent-last-sexp)
978 ;; This is the first line to start within the containing sexp.
979 ;; It's almost certainly a function call.
980 (if (= (point) calculate-lisp-indent-last-sexp)
981 ;; Containing sexp has nothing before this line
982 ;; except the first element. Indent under that element.
984 ;; Skip the first element, find start of second (the first
985 ;; argument of the function call) and indent under.
986 (progn (forward-sexp 1)
987 (parse-partial-sexp (point)
988 calculate-lisp-indent-last-sexp
990 (backward-prefix-chars))
992 ;; Indent beneath first sexp on same line as
993 ;; `calculate-lisp-indent-last-sexp'. Again, it's
994 ;; almost certainly a function call.
995 (goto-char calculate-lisp-indent-last-sexp)
997 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
999 (backward-prefix-chars)))))
1000 ;; Point is at the point to indent under unless we are inside a string.
1001 ;; Call indentation hook except when overridden by lisp-indent-offset
1002 ;; or if the desired indentation has already been computed.
1003 (let ((normal-indent (current-column)))
1004 (cond ((elt state 3)
1005 ;; Inside a string, don't change indentation.
1007 ((and (integerp lisp-indent-offset) containing-sexp)
1008 ;; Indent by constant offset
1009 (goto-char containing-sexp)
1010 (+ (current-column) lisp-indent-offset))
1011 ;; in this case calculate-lisp-indent-last-sexp is not nil
1012 (calculate-lisp-indent-last-sexp
1014 ;; try to align the parameters of a known function
1015 (and lisp-indent-function
1017 (funcall lisp-indent-function indent-point state))
1018 ;; If the function has no special alignment
1019 ;; or it does not apply to this argument,
1020 ;; try to align a constant-symbol under the last
1021 ;; preceding constant symbol, if there is such one of
1022 ;; the last 2 preceding symbols, in the previous
1023 ;; uncommented line.
1024 (and (save-excursion
1025 (goto-char indent-point)
1026 (skip-chars-forward " \t")
1028 ;; The last sexp may not be at the indentation
1029 ;; where it begins, so find that one, instead.
1031 (goto-char calculate-lisp-indent-last-sexp)
1032 (while (and (not (looking-back "^[ \t]*"))
1033 (or (not containing-sexp)
1034 (< (1+ containing-sexp) (point))))
1036 (backward-prefix-chars))
1037 (setq calculate-lisp-indent-last-sexp (point)))
1038 (> calculate-lisp-indent-last-sexp
1040 (goto-char (1+ containing-sexp))
1041 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1043 (let ((parse-sexp-ignore-comments t)
1045 (goto-char calculate-lisp-indent-last-sexp)
1046 (or (and (looking-at ":")
1047 (setq indent (current-column)))
1048 (and (< (save-excursion (beginning-of-line) (point))
1049 (prog2 (backward-sexp) (point)))
1051 (setq indent (current-column))))
1053 ;; another symbols or constants not preceded by a constant
1054 ;; as defined above.
1056 ;; in this case calculate-lisp-indent-last-sexp is nil
1061 (defun lisp-indent-function (indent-point state)
1062 "This function is the normal value of the variable `lisp-indent-function'.
1063 It is used when indenting a line within a function call, to see if the
1064 called function says anything special about how to indent the line.
1066 INDENT-POINT is the position where the user typed TAB, or equivalent.
1067 Point is located at the point to indent under (for default indentation);
1068 STATE is the `parse-partial-sexp' state for that position.
1070 If the current line is in a call to a Lisp function
1071 which has a non-nil property `lisp-indent-function',
1072 that specifies how to do the indentation. The property value can be
1073 * `defun', meaning indent `defun'-style;
1074 * an integer N, meaning indent the first N arguments specially
1075 like ordinary function arguments and then indent any further
1076 arguments like a body;
1077 * a function to call just as this function was called.
1078 If that function returns nil, that means it doesn't specify
1081 This function also returns nil meaning don't specify the indentation."
1082 (let ((normal-indent (current-column)))
1083 (goto-char (1+ (elt state 1)))
1084 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1085 (if (and (elt state 2)
1086 (not (looking-at "\\sw\\|\\s_")))
1087 ;; car of form doesn't seem to be a symbol
1089 (if (not (> (save-excursion (forward-line 1) (point))
1090 calculate-lisp-indent-last-sexp))
1091 (progn (goto-char calculate-lisp-indent-last-sexp)
1093 (parse-partial-sexp (point)
1094 calculate-lisp-indent-last-sexp 0 t)))
1095 ;; Indent under the list or under the first sexp on the same
1096 ;; line as calculate-lisp-indent-last-sexp. Note that first
1097 ;; thing on that line has to be complete sexp since we are
1098 ;; inside the innermost containing sexp.
1099 (backward-prefix-chars)
1101 (let ((function (buffer-substring (point)
1102 (progn (forward-sexp 1) (point))))
1104 (setq method (or (get (intern-soft function) 'lisp-indent-function)
1105 (get (intern-soft function) 'lisp-indent-hook)))
1106 (cond ((or (eq method 'defun)
1108 (> (length function) 3)
1109 (string-match "\\`def" function)))
1110 (lisp-indent-defform state indent-point))
1112 (lisp-indent-specform method state
1113 indent-point normal-indent))
1115 (funcall method indent-point state)))))))
1117 (defcustom lisp-body-indent 2
1118 "Number of columns to indent the second line of a `(def...)' form."
1121 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1123 (defun lisp-indent-specform (count state indent-point normal-indent)
1124 (let ((containing-form-start (elt state 1))
1126 body-indent containing-form-column)
1127 ;; Move to the start of containing form, calculate indentation
1128 ;; to use for non-distinguished forms (> count), and move past the
1129 ;; function symbol. lisp-indent-function guarantees that there is at
1130 ;; least one word or symbol character following open paren of containing
1132 (goto-char containing-form-start)
1133 (setq containing-form-column (current-column))
1134 (setq body-indent (+ lisp-body-indent containing-form-column))
1137 ;; Now find the start of the last form.
1138 (parse-partial-sexp (point) indent-point 1 t)
1139 (while (and (< (point) indent-point)
1142 (setq count (1- count))
1144 (parse-partial-sexp (point) indent-point 1 t))
1146 ;; Point is sitting on first character of last (or count) sexp.
1148 ;; A distinguished form. If it is the first or second form use double
1149 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1150 ;; to 2 (the default), this just happens to work the same with if as
1151 ;; the older code, but it makes unwind-protect, condition-case,
1152 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1153 ;; less hacked, behavior can be obtained by replacing below with
1154 ;; (list normal-indent containing-form-start).
1155 (if (<= (- i count) 1)
1156 (list (+ containing-form-column (* 2 lisp-body-indent))
1157 containing-form-start)
1158 (list normal-indent containing-form-start))
1159 ;; A non-distinguished form. Use body-indent if there are no
1160 ;; distinguished forms and this is the first undistinguished form,
1161 ;; or if this is the first undistinguished form and the preceding
1162 ;; distinguished form has indentation at least as great as body-indent.
1163 (if (or (and (= i 0) (= count 0))
1164 (and (= count 0) (<= body-indent normal-indent)))
1168 (defun lisp-indent-defform (state indent-point)
1169 (goto-char (car (cdr state)))
1171 (if (> (point) (car (cdr (cdr state))))
1173 (goto-char (car (cdr state)))
1174 (+ lisp-body-indent (current-column)))))
1177 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1178 ;; like defun if the first form is placed on the next line, otherwise
1179 ;; it is indented like any other form (i.e. forms line up under first).
1181 (put 'lambda 'lisp-indent-function 'defun)
1182 (put 'autoload 'lisp-indent-function 'defun)
1183 (put 'progn 'lisp-indent-function 0)
1184 (put 'prog1 'lisp-indent-function 1)
1185 (put 'prog2 'lisp-indent-function 2)
1186 (put 'save-excursion 'lisp-indent-function 0)
1187 (put 'save-window-excursion 'lisp-indent-function 0)
1188 (put 'save-selected-window 'lisp-indent-function 0)
1189 (put 'save-restriction 'lisp-indent-function 0)
1190 (put 'save-match-data 'lisp-indent-function 0)
1191 (put 'save-current-buffer 'lisp-indent-function 0)
1192 (put 'with-current-buffer 'lisp-indent-function 1)
1193 (put 'combine-after-change-calls 'lisp-indent-function 0)
1194 (put 'with-output-to-string 'lisp-indent-function 0)
1195 (put 'with-temp-file 'lisp-indent-function 1)
1196 (put 'with-temp-buffer 'lisp-indent-function 0)
1197 (put 'with-temp-message 'lisp-indent-function 1)
1198 (put 'with-syntax-table 'lisp-indent-function 1)
1199 (put 'let 'lisp-indent-function 1)
1200 (put 'let* 'lisp-indent-function 1)
1201 (put 'while 'lisp-indent-function 1)
1202 (put 'if 'lisp-indent-function 2)
1203 (put 'read-if 'lisp-indent-function 2)
1204 (put 'catch 'lisp-indent-function 1)
1205 (put 'condition-case 'lisp-indent-function 2)
1206 (put 'unwind-protect 'lisp-indent-function 1)
1207 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1208 (put 'eval-after-load 'lisp-indent-function 1)
1209 (put 'dolist 'lisp-indent-function 1)
1210 (put 'dotimes 'lisp-indent-function 1)
1211 (put 'when 'lisp-indent-function 1)
1212 (put 'unless 'lisp-indent-function 1)
1214 (defun indent-sexp (&optional endpos)
1215 "Indent each line of the list starting just after point.
1216 If optional arg ENDPOS is given, indent each line, stopping when
1217 ENDPOS is encountered."
1219 (let ((indent-stack (list nil))
1221 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1222 ;; so that calculate-lisp-indent will find the beginning of
1223 ;; the defun we are in.
1224 ;; If ENDPOS is nil, it is safe not to scan before point
1225 ;; since every line we indent is more deeply nested than point is.
1226 (starting-point (if endpos nil (point)))
1227 (last-point (point))
1228 last-depth bol outer-loop-done inner-loop-done state this-indent)
1230 ;; Get error now if we don't have a complete sexp after point.
1231 (save-excursion (forward-sexp 1)))
1233 (setq outer-loop-done nil)
1234 (while (if endpos (< (point) endpos)
1235 (not outer-loop-done))
1236 (setq last-depth next-depth
1237 inner-loop-done nil)
1238 ;; Parse this line so we can learn the state
1239 ;; to indent the next line.
1240 ;; This inner loop goes through only once
1241 ;; unless a line ends inside a string.
1242 (while (and (not inner-loop-done)
1243 (not (setq outer-loop-done (eobp))))
1244 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1246 (setq next-depth (car state))
1247 ;; If the line contains a comment other than the sort
1248 ;; that is indented like code,
1249 ;; indent it now with indent-for-comment.
1250 ;; Comments indented like code are right already.
1251 ;; In any case clear the in-comment flag in the state
1252 ;; because parse-partial-sexp never sees the newlines.
1253 (if (car (nthcdr 4 state))
1254 (progn (indent-for-comment)
1256 (setcar (nthcdr 4 state) nil)))
1257 ;; If this line ends inside a string,
1258 ;; go straight to next line, remaining within the inner loop,
1259 ;; and turn off the \-flag.
1260 (if (car (nthcdr 3 state))
1263 (setcar (nthcdr 5 state) nil))
1264 (setq inner-loop-done t)))
1268 (setq indent-stack (nconc indent-stack
1269 (make-list (- next-depth) nil))
1270 last-depth (- last-depth next-depth)
1273 ;; Decide whether to exit.
1275 ;; If we have already reached the specified end,
1276 ;; give up and do not reindent this line.
1277 (if (<= endpos (point))
1278 (setq outer-loop-done t))
1279 ;; If no specified end, we are done if we have finished one sexp.
1280 (if (<= next-depth 0)
1281 (setq outer-loop-done t)))
1282 (unless outer-loop-done
1283 (while (> last-depth next-depth)
1284 (setq indent-stack (cdr indent-stack)
1285 last-depth (1- last-depth)))
1286 (while (< last-depth next-depth)
1287 (setq indent-stack (cons nil indent-stack)
1288 last-depth (1+ last-depth)))
1289 ;; Now indent the next line according
1290 ;; to what we learned from parsing the previous one.
1292 (skip-chars-forward " \t")
1293 ;; But not if the line is blank, or just a comment
1294 ;; (except for double-semi comments; indent them as usual).
1295 (if (or (eobp) (looking-at "\\s<\\|\n"))
1297 (if (and (car indent-stack)
1298 (>= (car indent-stack) 0))
1299 (setq this-indent (car indent-stack))
1300 (let ((val (calculate-lisp-indent
1301 (if (car indent-stack) (- (car indent-stack))
1304 (setq this-indent val)
1306 (setcar indent-stack
1307 (setq this-indent val))
1308 (setcar indent-stack (- (car (cdr val))))
1309 (setq this-indent (car val))))))
1310 (if (and this-indent (/= (current-column) this-indent))
1311 (progn (delete-region bol (point))
1312 (indent-to this-indent)))))
1314 (setq outer-loop-done (= (point) last-point))
1315 (setq last-point (point)))))))
1317 (defun lisp-indent-region (start end)
1318 "Indent every line whose first char is between START and END inclusive."
1320 (let ((endmark (copy-marker end)))
1322 (and (bolp) (not (eolp))
1324 (indent-sexp endmark)
1325 (set-marker endmark nil))))
1327 (defun indent-pp-sexp (&optional arg)
1328 "Indent each line of the list starting just after point, or prettyprint it.
1329 A prefix argument specifies pretty-printing."
1334 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1336 (goto-char (point-max))
1337 (if (eq (char-before) ?\n)
1338 (delete-char -1)))))
1341 ;;;; Lisp paragraph filling commands.
1343 (defcustom emacs-lisp-docstring-fill-column 65
1344 "Value of `fill-column' to use when filling a docstring.
1345 Any non-integer value means do not use a different value of
1346 `fill-column' when filling docstrings."
1347 :type '(choice (integer)
1348 (const :tag "Use the current `fill-column'" t))
1351 (defun lisp-fill-paragraph (&optional justify)
1352 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1353 If any of the current line is a comment, fill the comment or the
1354 paragraph of it that point is in, preserving the comment's indentation
1355 and initial semicolons."
1357 (or (fill-comment-paragraph justify)
1358 ;; Since fill-comment-paragraph returned nil, that means we're not in
1359 ;; a comment: Point is on a program line; we are interested
1360 ;; particularly in docstring lines.
1362 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1363 ;; are buffer-local, but we avoid changing them so that they can be set
1364 ;; to make `forward-paragraph' and friends do something the user wants.
1366 ;; `paragraph-start': The `(' in the character alternative and the
1367 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1368 ;; sexps and backquoted sexps that follow a docstring from being filled
1369 ;; with the docstring. This setting has the consequence of inhibiting
1370 ;; filling many program lines that are not docstrings, which is sensible,
1371 ;; because the user probably asked to fill program lines by accident, or
1372 ;; expecting indentation (perhaps we should try to do indenting in that
1373 ;; case). The `;' and `:' stop the paragraph being filled at following
1374 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1375 ;; escaped to keep font-locking, filling, & paren matching in the source
1378 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1379 ;; a docstring and identifies it as a paragraph separator, so that it
1380 ;; won't be filled. (Since the first line of documentation stands alone
1381 ;; in some contexts, filling should not alter the contents the author has
1382 ;; chosen.) Only the first line of a docstring begins with whitespace
1383 ;; and a quotation mark and ends with a period or (rarely) a comma.
1385 ;; The `fill-column' is temporarily bound to
1386 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1387 (let ((paragraph-start (concat paragraph-start
1388 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1390 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1391 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1392 (derived-mode-p 'emacs-lisp-mode))
1393 emacs-lisp-docstring-fill-column
1395 (fill-paragraph justify))
1396 ;; Never return nil.
1399 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1400 "Indent all lines of code, starting in the region, sideways by ARG columns.
1401 Does not affect lines starting inside comments or strings, assuming that
1402 the start of the region is not inside them.
1404 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1405 The last is a regexp which, if matched at the beginning of a line,
1406 means don't indent that line."
1407 (interactive "r\np")
1411 (setq end (point-marker))
1414 (setq state (parse-partial-sexp (point)
1416 (forward-line 1) (point))
1418 (while (< (point) end)
1419 (or (car (nthcdr 3 state))
1420 (and nochange-regexp
1421 (looking-at nochange-regexp))
1422 ;; If line does not start in string, indent it
1423 (let ((indent (current-indentation)))
1424 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1426 (indent-to (max 0 (+ indent arg)) 0))))
1427 (setq state (parse-partial-sexp (point)
1429 (forward-line 1) (point))
1432 (provide 'lisp-mode)
1434 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1435 ;;; lisp-mode.el ends here