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, 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))
83 (defvar lisp-mode-syntax-table
84 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
85 (modify-syntax-entry ?\[ "_ " table)
86 (modify-syntax-entry ?\] "_ " table)
87 (modify-syntax-entry ?# "' 14b" table)
88 (modify-syntax-entry ?| "\" 23bn" table)
91 (defvar lisp-imenu-generic-expression
94 (purecopy (concat "^\\s-*("
97 '("defun" "defun*" "defsubst" "defmacro"
98 "defadvice" "define-skeleton"
99 "define-minor-mode" "define-global-minor-mode"
100 "define-globalized-minor-mode"
101 "define-derived-mode" "define-generic-mode"
102 "define-compiler-macro" "define-modify-macro"
103 "defsetf" "define-setf-expander"
104 "define-method-combination"
105 "defgeneric" "defmethod") t))
106 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
108 (list (purecopy "Variables")
109 (purecopy (concat "^\\s-*("
112 '("defvar" "defconst" "defconstant" "defcustom"
113 "defparameter" "define-symbol-macro") t))
114 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
116 (list (purecopy "Types")
117 (purecopy (concat "^\\s-*("
120 '("defgroup" "deftheme" "deftype" "defstruct"
121 "defclass" "define-condition" "define-widget"
122 "defface" "defpackage") t))
123 "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
126 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
128 ;; This was originally in autoload.el and is still used there.
129 (put 'autoload 'doc-string-elt 3)
130 (put 'defun 'doc-string-elt 3)
131 (put 'defun* 'doc-string-elt 3)
132 (put 'defvar 'doc-string-elt 3)
133 (put 'defcustom 'doc-string-elt 3)
134 (put 'deftheme 'doc-string-elt 2)
135 (put 'defconst 'doc-string-elt 3)
136 (put 'defmacro 'doc-string-elt 3)
137 (put 'defmacro* 'doc-string-elt 3)
138 (put 'defsubst 'doc-string-elt 3)
139 (put 'defstruct 'doc-string-elt 2)
140 (put 'define-skeleton 'doc-string-elt 2)
141 (put 'define-derived-mode 'doc-string-elt 4)
142 (put 'define-compilation-mode 'doc-string-elt 3)
143 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
144 (put 'define-minor-mode 'doc-string-elt 2)
145 (put 'easy-mmode-define-global-mode 'doc-string-elt 2)
146 (put 'define-global-minor-mode 'doc-string-elt 2)
147 (put 'define-globalized-minor-mode 'doc-string-elt 2)
148 (put 'define-generic-mode 'doc-string-elt 7)
149 (put 'define-ibuffer-filter 'doc-string-elt 2)
150 (put 'define-ibuffer-op 'doc-string-elt 3)
151 (put 'define-ibuffer-sorter 'doc-string-elt 2)
152 (put 'lambda 'doc-string-elt 2)
153 (put 'defalias 'doc-string-elt 3)
154 (put 'defvaralias 'doc-string-elt 3)
155 (put 'define-category 'doc-string-elt 2)
157 (defvar lisp-doc-string-elt-property 'doc-string-elt
158 "The symbol property that holds the docstring position info.")
160 (defun lisp-font-lock-syntactic-face-function (state)
162 ;; This might be a (doc)string or a |...| symbol.
163 (let ((startpos (nth 8 state)))
164 (if (eq (char-after startpos) ?|)
165 ;; This is not a string, but a |...| symbol.
167 (let* ((listbeg (nth 1 state))
168 (firstsym (and listbeg
171 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
173 (docelt (and firstsym (get (intern-soft firstsym)
174 lisp-doc-string-elt-property))))
176 ;; It's a string in a form that can have a docstring.
177 ;; Check whether it's in docstring position.
179 (when (functionp docelt)
180 (goto-char (match-end 1))
181 (setq docelt (funcall docelt)))
185 (while (and (> docelt 0) (< (point) startpos)
186 (progn (forward-sexp 1) t))
187 (setq docelt (1- docelt)))
189 (and (zerop docelt) (<= (point) startpos)
190 (progn (forward-comment (point-max)) t)
191 (= (point) (nth 8 state)))))
193 font-lock-string-face))))
194 font-lock-comment-face))
196 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive)
197 "Common initialization routine for lisp modes.
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 KEYWORDS-CASE-SENSITIVE means that for font-lock keywords will not be case sensitive."
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 ,keywords-case-insensitive (("+-*/.<>=!?$%_&~^:@" . "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 "Untrace all currently traced functions"))
295 (define-key tracing-map [tr-uf]
296 '(menu-item "Untrace function..." untrace-function
297 :help "Untrace function, and possibly activate 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 an 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.
420 \\{emacs-lisp-mode-map}
421 Entry to this mode calls the value of `emacs-lisp-mode-hook'
422 if that value is non-nil."
424 (kill-all-local-variables)
425 (use-local-map emacs-lisp-mode-map)
426 (set-syntax-table emacs-lisp-mode-syntax-table)
427 (setq major-mode 'emacs-lisp-mode)
428 (setq mode-name "Emacs-Lisp")
429 (lisp-mode-variables)
430 (setq imenu-case-fold-search nil)
431 (run-mode-hooks 'emacs-lisp-mode-hook))
432 (put 'emacs-lisp-mode 'custom-mode-group 'lisp)
434 (defvar lisp-mode-map
435 (let ((map (make-sparse-keymap))
436 (menu-map (make-sparse-keymap "Lisp")))
437 (set-keymap-parent map lisp-mode-shared-map)
438 (define-key map "\e\C-x" 'lisp-eval-defun)
439 (define-key map "\C-c\C-z" 'run-lisp)
440 (define-key map [menu-bar lisp] (cons "Lisp" menu-map))
441 (define-key menu-map [run-lisp]
442 '(menu-item "Run inferior Lisp" run-lisp
443 :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))
444 (define-key menu-map [ev-def]
445 '(menu-item "Eval defun" lisp-eval-defun
446 :help "Send the current defun to the Lisp process made by M-x run-lisp"))
447 (define-key menu-map [ind-sexp]
448 '(menu-item "Indent sexp" indent-sexp
449 :help "Indent each line of the list starting just after point"))
451 "Keymap for ordinary Lisp mode.
452 All commands in `lisp-mode-shared-map' are inherited by this map.")
455 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
457 Delete converts tabs to spaces as it moves back.
458 Blank lines separate paragraphs. Semicolons start comments.
461 Note that `run-lisp' may be used either to start an inferior Lisp job
462 or to switch back to an existing one.
464 Entry to this mode calls the value of `lisp-mode-hook'
465 if that value is non-nil."
467 (kill-all-local-variables)
468 (use-local-map lisp-mode-map)
469 (setq major-mode 'lisp-mode)
470 (setq mode-name "Lisp")
471 (lisp-mode-variables nil t)
472 (make-local-variable 'comment-start-skip)
473 (setq comment-start-skip
474 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
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.
538 \\{lisp-interaction-mode-map}
539 Entry to this mode calls the value of `lisp-interaction-mode-hook'
540 if that value is non-nil.")
542 (defun eval-print-last-sexp ()
543 "Evaluate sexp before point; print value into current buffer.
545 If `eval-expression-debug-on-error' is non-nil, which is the default,
546 this command arranges for all errors to enter the debugger.
548 Note that printing the result is controlled by the variables
549 `eval-expression-print-length' and `eval-expression-print-level',
552 (let ((standard-output (current-buffer)))
558 (defun last-sexp-setup-props (beg end value alt1 alt2)
559 "Set up text properties for the output of `eval-last-sexp-1'.
560 BEG and END are the start and end of the output in current-buffer.
561 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
562 alternative printed representations that can be displayed."
563 (let ((map (make-sparse-keymap)))
564 (define-key map "\C-m" 'last-sexp-toggle-display)
565 (define-key map [down-mouse-2] 'mouse-set-point)
566 (define-key map [mouse-2] 'last-sexp-toggle-display)
569 `(printed-value (,value ,alt1 ,alt2)
572 help-echo "RET, mouse-2: toggle abbreviated display"
573 rear-nonsticky (mouse-face keymap help-echo
577 (defun last-sexp-toggle-display (&optional arg)
578 "Toggle between abbreviated and unabbreviated printed representations."
582 (let ((value (get-text-property (point) 'printed-value)))
584 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
587 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
588 (standard-output (current-buffer))
590 (delete-region beg end)
591 (insert (nth 1 value))
593 (setq point (1- (point))))
594 (last-sexp-setup-props beg (point)
598 (goto-char (min (point-max) point)))))))
600 (defun prin1-char (char)
601 "Return a string representing CHAR as a character rather than as an integer.
602 If CHAR is not a character, return nil."
605 (let ((c (event-basic-type char))
606 (mods (event-modifiers char))
608 ;; Prevent ?A from turning into ?\S-a.
609 (if (and (memq 'shift mods)
610 (zerop (logand char ?\S-\^@))
611 (not (let ((case-fold-search nil))
612 (char-equal c (upcase c)))))
613 (setq c (upcase c) mods nil))
614 ;; What string are we considering using?
621 (cond ((eq modif 'super) "\\s-")
622 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
625 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
630 ;; Verify the string reads a CHAR, not to some other character.
631 ;; If it doesn't, return nil instead.
633 (= (car (read-from-string string)) char)
637 (defun preceding-sexp ()
638 "Return sexp before the point."
639 (let ((opoint (point))
643 (with-syntax-table emacs-lisp-mode-syntax-table
644 ;; If this sexp appears to be enclosed in `...'
645 ;; then ignore the surrounding quotes.
647 (or (eq (following-char) ?\')
648 (eq (preceding-char) ?\')))
650 ;; If we were after `?\e' (or similar case),
651 ;; use the whole thing, not just the `e'.
652 (when (eq (preceding-char) ?\\)
654 (when (eq (preceding-char) ??)
658 (when (eq (preceding-char) ?=)
661 (skip-chars-backward "0-9#=")
662 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
667 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
668 ;; `variable' so that the value is returned, not the
670 (if (and ignore-quotes
671 (eq (following-char) ?`))
673 (narrow-to-region (point-min) opoint)
674 (setq expr (read (current-buffer)))
675 ;; If it's an (interactive ...) form, it's more
676 ;; useful to show how an interactive call would
679 (eq (car expr) 'interactive)
681 (list 'call-interactively
690 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
691 "Evaluate sexp before point; print value in minibuffer.
692 With argument, print output into current buffer."
693 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
694 (eval-last-sexp-print-value (eval (preceding-sexp)))))
697 (defun eval-last-sexp-print-value (value)
698 (let ((unabbreviated (let ((print-length nil) (print-level nil))
699 (prin1-to-string value)))
700 (print-length eval-expression-print-length)
701 (print-level eval-expression-print-level)
706 (let ((str (eval-expression-print-format value)))
707 (if str (princ str)))
709 (when (and (bufferp standard-output)
710 (or (not (null print-length))
711 (not (null print-level)))
712 (not (string= unabbreviated
713 (buffer-substring-no-properties beg end))))
714 (last-sexp-setup-props beg end value
716 (buffer-substring-no-properties beg end))
720 (defvar eval-last-sexp-fake-value (make-symbol "t"))
722 (defun eval-last-sexp (eval-last-sexp-arg-internal)
723 "Evaluate sexp before point; print value in minibuffer.
724 Interactively, with prefix argument, print output into current buffer.
726 If `eval-expression-debug-on-error' is non-nil, which is the default,
727 this command arranges for all errors to enter the debugger."
729 (if (null eval-expression-debug-on-error)
730 (eval-last-sexp-1 eval-last-sexp-arg-internal)
732 (let ((debug-on-error eval-last-sexp-fake-value))
733 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
735 (unless (eq (cdr value) eval-last-sexp-fake-value)
736 (setq debug-on-error (cdr value)))
739 (defun eval-defun-1 (form)
740 "Treat some expressions specially.
741 Reset the `defvar' and `defcustom' variables to the initial value.
742 Reinitialize the face according to the `defface' specification."
743 ;; The code in edebug-defun should be consistent with this, but not
744 ;; the same, since this gets a macroexpended form.
745 (cond ((not (listp form))
747 ((and (eq (car form) 'defvar)
748 (cdr-safe (cdr-safe form))
749 (boundp (cadr form)))
750 ;; Force variable to be re-set.
751 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
752 (setq-default ,(nth 1 form) ,(nth 2 form))))
753 ;; `defcustom' is now macroexpanded to
754 ;; `custom-declare-variable' with a quoted value arg.
755 ((and (eq (car form) 'custom-declare-variable)
756 (default-boundp (eval (nth 1 form))))
757 ;; Force variable to be bound.
758 (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form))))
760 ;; `defface' is macroexpanded to `custom-declare-face'.
761 ((eq (car form) 'custom-declare-face)
763 (setq face-new-frame-defaults
764 (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults))
765 (put (eval (nth 1 form)) 'face-defface-spec nil)
766 ;; Setting `customized-face' to the new spec after calling
767 ;; the form, but preserving the old saved spec in `saved-face',
768 ;; imitates the situation when the new face spec is set
769 ;; temporarily for the current session in the customize
770 ;; buffer, thus allowing `face-user-default-spec' to use the
771 ;; new customized spec instead of the saved spec.
772 ;; Resetting `saved-face' temporarily to nil is needed to let
773 ;; `defface' change the spec, regardless of a saved spec.
775 (put ,(nth 1 form) 'saved-face
776 ',(get (eval (nth 1 form)) 'saved-face))
777 (put ,(nth 1 form) 'customized-face
779 (put (eval (nth 1 form)) 'saved-face nil)))
780 ((eq (car form) 'progn)
781 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
784 (defun eval-defun-2 ()
785 "Evaluate defun that point is in or before.
786 The value is displayed in the minibuffer.
787 If the current defun is actually a call to `defvar',
788 then reset the variable using the initial value expression
789 even if the variable already has some other value.
790 \(Normally `defvar' does not change the variable's value
791 if it already has a value.\)
793 With argument, insert value in current buffer after the defun.
794 Return the result of evaluation."
796 ;; FIXME: the print-length/level bindings should only be applied while
797 ;; printing, not while evaluating.
798 (let ((debug-on-error eval-expression-debug-on-error)
799 (print-length eval-expression-print-length)
800 (print-level eval-expression-print-level))
802 ;; Arrange for eval-region to "read" the (possibly) altered form.
803 ;; eval-region handles recording which file defines a function or
804 ;; variable. Re-written using `apply' to avoid capturing
805 ;; variables like `end'.
808 (let ((standard-output t)
810 ;; Read the form from the buffer, and record where it ends.
815 (setq form (read (current-buffer)))
817 ;; Alter the form if necessary.
818 (setq form (eval-defun-1 (macroexpand form)))
819 (list beg end standard-output
821 ;; Skipping to the end of the specified region
822 ;; will make eval-region return.
825 ;; The result of evaluation has been put onto VALUES. So return it.
828 (defun eval-defun (edebug-it)
829 "Evaluate the top-level form containing point, or after point.
831 If the current defun is actually a call to `defvar' or `defcustom',
832 evaluating it this way resets the variable using its initial value
833 expression even if the variable already has some other value.
834 \(Normally `defvar' and `defcustom' do not alter the value if there
835 already is one.) In an analogous way, evaluating a `defface'
836 overrides any customizations of the face, so that it becomes
837 defined exactly as the `defface' expression says.
839 If `eval-expression-debug-on-error' is non-nil, which is the default,
840 this command arranges for all errors to enter the debugger.
842 With a prefix argument, instrument the code for Edebug.
844 If acting on a `defun' for FUNCTION, and the function was
845 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
846 instrumented, just FUNCTION is printed.
848 If not acting on a `defun', the result of evaluation is displayed in
849 the minibuffer. This display is controlled by the variables
850 `eval-expression-print-length' and `eval-expression-print-level',
855 (eval-defun (not edebug-all-defs)))
857 (if (null eval-expression-debug-on-error)
859 (let ((old-value (make-symbol "t")) new-value value)
860 (let ((debug-on-error old-value))
861 (setq value (eval-defun-2))
862 (setq new-value debug-on-error))
863 (unless (eq old-value new-value)
864 (setq debug-on-error new-value))
867 ;; May still be used by some external Lisp-mode variant.
868 (define-obsolete-function-alias 'lisp-comment-indent
869 'comment-indent-default "22.1")
870 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
872 (defcustom lisp-indent-offset nil
873 "If non-nil, indent second line of expressions that many more columns."
875 :type '(choice (const nil) integer))
876 (put 'lisp-body-indent 'safe-local-variable
877 (lambda (x) (or (null x) (integerp x))))
879 (defvar lisp-indent-function 'lisp-indent-function)
881 (defun lisp-indent-line (&optional whole-exp)
882 "Indent current line as Lisp code.
883 With argument, indent any additional lines of the same expression
884 rigidly along with this one."
886 (let ((indent (calculate-lisp-indent)) shift-amt end
887 (pos (- (point-max) (point)))
888 (beg (progn (beginning-of-line) (point))))
889 (skip-chars-forward " \t")
890 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
891 ;; Don't alter indentation of a ;;; comment line
892 ;; or a line that starts in a string.
893 (goto-char (- (point-max) pos))
894 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
895 ;; Single-semicolon comment lines should be indented
896 ;; as comment lines, not as code.
897 (progn (indent-for-comment) (forward-char -1))
898 (if (listp indent) (setq indent (car indent)))
899 (setq shift-amt (- indent (current-column)))
900 (if (zerop shift-amt)
902 (delete-region beg (point))
904 ;; If initial point was within line's indentation,
905 ;; position after the indentation. Else stay at same point in text.
906 (if (> (- (point-max) pos) (point))
907 (goto-char (- (point-max) pos)))
908 ;; If desired, shift remaining lines of expression the same amount.
909 (and whole-exp (not (zerop shift-amt))
918 (indent-code-rigidly beg end shift-amt)))))
920 (defvar calculate-lisp-indent-last-sexp)
922 (defun calculate-lisp-indent (&optional parse-start)
923 "Return appropriate indentation for current line as Lisp code.
924 In usual case returns an integer: the column to indent to.
925 If the value is nil, that means don't change the indentation
926 because the line starts inside a string.
928 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
929 This means that following lines at the same level of indentation
930 should not necessarily be indented the same as this line.
931 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
932 is the buffer position of the start of the containing expression."
935 (let ((indent-point (point))
937 ;; setting this to a number inhibits calling hook
940 calculate-lisp-indent-last-sexp containing-sexp)
942 (goto-char parse-start)
943 (beginning-of-defun))
944 ;; Find outermost containing sexp
945 (while (< (point) indent-point)
946 (setq state (parse-partial-sexp (point) indent-point 0)))
947 ;; Find innermost containing sexp
950 (> (setq paren-depth (elt state 0)) 0))
952 (setq calculate-lisp-indent-last-sexp (elt state 2))
953 (setq containing-sexp (elt state 1))
954 ;; Position following last unclosed open.
955 (goto-char (1+ containing-sexp))
956 ;; Is there a complete sexp since then?
957 (if (and calculate-lisp-indent-last-sexp
958 (> calculate-lisp-indent-last-sexp (point)))
959 ;; Yes, but is there a containing sexp after that?
960 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
962 (if (setq retry (car (cdr peek))) (setq state peek)))))
965 ;; Innermost containing sexp found
966 (goto-char (1+ containing-sexp))
967 (if (not calculate-lisp-indent-last-sexp)
968 ;; indent-point immediately follows open paren.
970 (setq desired-indent (current-column))
971 ;; Find the start of first element of containing sexp.
972 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
973 (cond ((looking-at "\\s(")
974 ;; First element of containing sexp is a list.
975 ;; Indent under that list.
977 ((> (save-excursion (forward-line 1) (point))
978 calculate-lisp-indent-last-sexp)
979 ;; This is the first line to start within the containing sexp.
980 ;; It's almost certainly a function call.
981 (if (= (point) calculate-lisp-indent-last-sexp)
982 ;; Containing sexp has nothing before this line
983 ;; except the first element. Indent under that element.
985 ;; Skip the first element, find start of second (the first
986 ;; argument of the function call) and indent under.
987 (progn (forward-sexp 1)
988 (parse-partial-sexp (point)
989 calculate-lisp-indent-last-sexp
991 (backward-prefix-chars))
993 ;; Indent beneath first sexp on same line as
994 ;; `calculate-lisp-indent-last-sexp'. Again, it's
995 ;; almost certainly a function call.
996 (goto-char calculate-lisp-indent-last-sexp)
998 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
1000 (backward-prefix-chars)))))
1001 ;; Point is at the point to indent under unless we are inside a string.
1002 ;; Call indentation hook except when overridden by lisp-indent-offset
1003 ;; or if the desired indentation has already been computed.
1004 (let ((normal-indent (current-column)))
1005 (cond ((elt state 3)
1006 ;; Inside a string, don't change indentation.
1008 ((and (integerp lisp-indent-offset) containing-sexp)
1009 ;; Indent by constant offset
1010 (goto-char containing-sexp)
1011 (+ (current-column) lisp-indent-offset))
1012 ;; in this case calculate-lisp-indent-last-sexp is not nil
1013 (calculate-lisp-indent-last-sexp
1015 ;; try to align the parameters of a known function
1016 (and lisp-indent-function
1018 (funcall lisp-indent-function indent-point state))
1019 ;; If the function has no special alignment
1020 ;; or it does not apply to this argument,
1021 ;; try to align a constant-symbol under the last
1022 ;; preceding constant symbol, if there is such one of
1023 ;; the last 2 preceding symbols, in the previous
1024 ;; uncommented line.
1025 (and (save-excursion
1026 (goto-char indent-point)
1027 (skip-chars-forward " \t")
1029 ;; The last sexp may not be at the indentation
1030 ;; where it begins, so find that one, instead.
1032 (goto-char calculate-lisp-indent-last-sexp)
1033 ;; Handle prefix characters and whitespace
1034 ;; following an open paren. (Bug#1012)
1035 (backward-prefix-chars)
1036 (while (and (not (looking-back "^[ \t]*\\|([ \t]+"))
1037 (or (not containing-sexp)
1038 (< (1+ containing-sexp) (point))))
1040 (backward-prefix-chars))
1041 (setq calculate-lisp-indent-last-sexp (point)))
1042 (> calculate-lisp-indent-last-sexp
1044 (goto-char (1+ containing-sexp))
1045 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1047 (let ((parse-sexp-ignore-comments t)
1049 (goto-char calculate-lisp-indent-last-sexp)
1050 (or (and (looking-at ":")
1051 (setq indent (current-column)))
1052 (and (< (save-excursion (beginning-of-line) (point))
1053 (prog2 (backward-sexp) (point)))
1055 (setq indent (current-column))))
1057 ;; another symbols or constants not preceded by a constant
1058 ;; as defined above.
1060 ;; in this case calculate-lisp-indent-last-sexp is nil
1065 (defun lisp-indent-function (indent-point state)
1066 "This function is the normal value of the variable `lisp-indent-function'.
1067 It is used when indenting a line within a function call, to see if the
1068 called function says anything special about how to indent the line.
1070 INDENT-POINT is the position where the user typed TAB, or equivalent.
1071 Point is located at the point to indent under (for default indentation);
1072 STATE is the `parse-partial-sexp' state for that position.
1074 If the current line is in a call to a Lisp function
1075 which has a non-nil property `lisp-indent-function',
1076 that specifies how to do the indentation. The property value can be
1077 * `defun', meaning indent `defun'-style;
1078 * an integer N, meaning indent the first N arguments specially
1079 like ordinary function arguments and then indent any further
1080 arguments like a body;
1081 * a function to call just as this function was called.
1082 If that function returns nil, that means it doesn't specify
1085 This function also returns nil meaning don't specify the indentation."
1086 (let ((normal-indent (current-column)))
1087 (goto-char (1+ (elt state 1)))
1088 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1089 (if (and (elt state 2)
1090 (not (looking-at "\\sw\\|\\s_")))
1091 ;; car of form doesn't seem to be a symbol
1093 (if (not (> (save-excursion (forward-line 1) (point))
1094 calculate-lisp-indent-last-sexp))
1095 (progn (goto-char calculate-lisp-indent-last-sexp)
1097 (parse-partial-sexp (point)
1098 calculate-lisp-indent-last-sexp 0 t)))
1099 ;; Indent under the list or under the first sexp on the same
1100 ;; line as calculate-lisp-indent-last-sexp. Note that first
1101 ;; thing on that line has to be complete sexp since we are
1102 ;; inside the innermost containing sexp.
1103 (backward-prefix-chars)
1105 (let ((function (buffer-substring (point)
1106 (progn (forward-sexp 1) (point))))
1108 (setq method (or (get (intern-soft function) 'lisp-indent-function)
1109 (get (intern-soft function) 'lisp-indent-hook)))
1110 (cond ((or (eq method 'defun)
1112 (> (length function) 3)
1113 (string-match "\\`def" function)))
1114 (lisp-indent-defform state indent-point))
1116 (lisp-indent-specform method state
1117 indent-point normal-indent))
1119 (funcall method indent-point state)))))))
1121 (defcustom lisp-body-indent 2
1122 "Number of columns to indent the second line of a `(def...)' form."
1125 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1127 (defun lisp-indent-specform (count state indent-point normal-indent)
1128 (let ((containing-form-start (elt state 1))
1130 body-indent containing-form-column)
1131 ;; Move to the start of containing form, calculate indentation
1132 ;; to use for non-distinguished forms (> count), and move past the
1133 ;; function symbol. lisp-indent-function guarantees that there is at
1134 ;; least one word or symbol character following open paren of containing
1136 (goto-char containing-form-start)
1137 (setq containing-form-column (current-column))
1138 (setq body-indent (+ lisp-body-indent containing-form-column))
1141 ;; Now find the start of the last form.
1142 (parse-partial-sexp (point) indent-point 1 t)
1143 (while (and (< (point) indent-point)
1146 (setq count (1- count))
1148 (parse-partial-sexp (point) indent-point 1 t))
1150 ;; Point is sitting on first character of last (or count) sexp.
1152 ;; A distinguished form. If it is the first or second form use double
1153 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1154 ;; to 2 (the default), this just happens to work the same with if as
1155 ;; the older code, but it makes unwind-protect, condition-case,
1156 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1157 ;; less hacked, behavior can be obtained by replacing below with
1158 ;; (list normal-indent containing-form-start).
1159 (if (<= (- i count) 1)
1160 (list (+ containing-form-column (* 2 lisp-body-indent))
1161 containing-form-start)
1162 (list normal-indent containing-form-start))
1163 ;; A non-distinguished form. Use body-indent if there are no
1164 ;; distinguished forms and this is the first undistinguished form,
1165 ;; or if this is the first undistinguished form and the preceding
1166 ;; distinguished form has indentation at least as great as body-indent.
1167 (if (or (and (= i 0) (= count 0))
1168 (and (= count 0) (<= body-indent normal-indent)))
1172 (defun lisp-indent-defform (state indent-point)
1173 (goto-char (car (cdr state)))
1175 (if (> (point) (car (cdr (cdr state))))
1177 (goto-char (car (cdr state)))
1178 (+ lisp-body-indent (current-column)))))
1181 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1182 ;; like defun if the first form is placed on the next line, otherwise
1183 ;; it is indented like any other form (i.e. forms line up under first).
1185 (put 'lambda 'lisp-indent-function 'defun)
1186 (put 'autoload 'lisp-indent-function 'defun)
1187 (put 'progn 'lisp-indent-function 0)
1188 (put 'prog1 'lisp-indent-function 1)
1189 (put 'prog2 'lisp-indent-function 2)
1190 (put 'save-excursion 'lisp-indent-function 0)
1191 (put 'save-window-excursion 'lisp-indent-function 0)
1192 (put 'save-selected-window 'lisp-indent-function 0)
1193 (put 'save-restriction 'lisp-indent-function 0)
1194 (put 'save-match-data 'lisp-indent-function 0)
1195 (put 'save-current-buffer 'lisp-indent-function 0)
1196 (put 'with-current-buffer 'lisp-indent-function 1)
1197 (put 'combine-after-change-calls 'lisp-indent-function 0)
1198 (put 'with-output-to-string 'lisp-indent-function 0)
1199 (put 'with-temp-file 'lisp-indent-function 1)
1200 (put 'with-temp-buffer 'lisp-indent-function 0)
1201 (put 'with-temp-message 'lisp-indent-function 1)
1202 (put 'with-syntax-table 'lisp-indent-function 1)
1203 (put 'let 'lisp-indent-function 1)
1204 (put 'let* 'lisp-indent-function 1)
1205 (put 'while 'lisp-indent-function 1)
1206 (put 'if 'lisp-indent-function 2)
1207 (put 'read-if 'lisp-indent-function 2)
1208 (put 'catch 'lisp-indent-function 1)
1209 (put 'condition-case 'lisp-indent-function 2)
1210 (put 'unwind-protect 'lisp-indent-function 1)
1211 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1212 (put 'eval-after-load 'lisp-indent-function 1)
1213 (put 'dolist 'lisp-indent-function 1)
1214 (put 'dotimes 'lisp-indent-function 1)
1215 (put 'when 'lisp-indent-function 1)
1216 (put 'unless 'lisp-indent-function 1)
1218 (defun indent-sexp (&optional endpos)
1219 "Indent each line of the list starting just after point.
1220 If optional arg ENDPOS is given, indent each line, stopping when
1221 ENDPOS is encountered."
1223 (let ((indent-stack (list nil))
1225 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1226 ;; so that calculate-lisp-indent will find the beginning of
1227 ;; the defun we are in.
1228 ;; If ENDPOS is nil, it is safe not to scan before point
1229 ;; since every line we indent is more deeply nested than point is.
1230 (starting-point (if endpos nil (point)))
1231 (last-point (point))
1232 last-depth bol outer-loop-done inner-loop-done state this-indent)
1234 ;; Get error now if we don't have a complete sexp after point.
1235 (save-excursion (forward-sexp 1)))
1237 (setq outer-loop-done nil)
1238 (while (if endpos (< (point) endpos)
1239 (not outer-loop-done))
1240 (setq last-depth next-depth
1241 inner-loop-done nil)
1242 ;; Parse this line so we can learn the state
1243 ;; to indent the next line.
1244 ;; This inner loop goes through only once
1245 ;; unless a line ends inside a string.
1246 (while (and (not inner-loop-done)
1247 (not (setq outer-loop-done (eobp))))
1248 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1250 (setq next-depth (car state))
1251 ;; If the line contains a comment other than the sort
1252 ;; that is indented like code,
1253 ;; indent it now with indent-for-comment.
1254 ;; Comments indented like code are right already.
1255 ;; In any case clear the in-comment flag in the state
1256 ;; because parse-partial-sexp never sees the newlines.
1257 (if (car (nthcdr 4 state))
1258 (progn (indent-for-comment)
1260 (setcar (nthcdr 4 state) nil)))
1261 ;; If this line ends inside a string,
1262 ;; go straight to next line, remaining within the inner loop,
1263 ;; and turn off the \-flag.
1264 (if (car (nthcdr 3 state))
1267 (setcar (nthcdr 5 state) nil))
1268 (setq inner-loop-done t)))
1272 (setq indent-stack (nconc indent-stack
1273 (make-list (- next-depth) nil))
1274 last-depth (- last-depth next-depth)
1277 ;; Decide whether to exit.
1279 ;; If we have already reached the specified end,
1280 ;; give up and do not reindent this line.
1281 (if (<= endpos (point))
1282 (setq outer-loop-done t))
1283 ;; If no specified end, we are done if we have finished one sexp.
1284 (if (<= next-depth 0)
1285 (setq outer-loop-done t)))
1286 (unless outer-loop-done
1287 (while (> last-depth next-depth)
1288 (setq indent-stack (cdr indent-stack)
1289 last-depth (1- last-depth)))
1290 (while (< last-depth next-depth)
1291 (setq indent-stack (cons nil indent-stack)
1292 last-depth (1+ last-depth)))
1293 ;; Now indent the next line according
1294 ;; to what we learned from parsing the previous one.
1296 (skip-chars-forward " \t")
1297 ;; But not if the line is blank, or just a comment
1298 ;; (except for double-semi comments; indent them as usual).
1299 (if (or (eobp) (looking-at "\\s<\\|\n"))
1301 (if (and (car indent-stack)
1302 (>= (car indent-stack) 0))
1303 (setq this-indent (car indent-stack))
1304 (let ((val (calculate-lisp-indent
1305 (if (car indent-stack) (- (car indent-stack))
1308 (setq this-indent val)
1310 (setcar indent-stack
1311 (setq this-indent val))
1312 (setcar indent-stack (- (car (cdr val))))
1313 (setq this-indent (car val))))))
1314 (if (and this-indent (/= (current-column) this-indent))
1315 (progn (delete-region bol (point))
1316 (indent-to this-indent)))))
1318 (setq outer-loop-done (= (point) last-point))
1319 (setq last-point (point)))))))
1321 (defun lisp-indent-region (start end)
1322 "Indent every line whose first char is between START and END inclusive."
1324 (let ((endmark (copy-marker end)))
1326 (and (bolp) (not (eolp))
1328 (indent-sexp endmark)
1329 (set-marker endmark nil))))
1331 (defun indent-pp-sexp (&optional arg)
1332 "Indent each line of the list starting just after point, or prettyprint it.
1333 A prefix argument specifies pretty-printing."
1338 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1340 (goto-char (point-max))
1341 (if (eq (char-before) ?\n)
1342 (delete-char -1)))))
1345 ;;;; Lisp paragraph filling commands.
1347 (defcustom emacs-lisp-docstring-fill-column 65
1348 "Value of `fill-column' to use when filling a docstring.
1349 Any non-integer value means do not use a different value of
1350 `fill-column' when filling docstrings."
1351 :type '(choice (integer)
1352 (const :tag "Use the current `fill-column'" t))
1355 (defun lisp-fill-paragraph (&optional justify)
1356 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1357 If any of the current line is a comment, fill the comment or the
1358 paragraph of it that point is in, preserving the comment's indentation
1359 and initial semicolons."
1361 (or (fill-comment-paragraph justify)
1362 ;; Since fill-comment-paragraph returned nil, that means we're not in
1363 ;; a comment: Point is on a program line; we are interested
1364 ;; particularly in docstring lines.
1366 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1367 ;; are buffer-local, but we avoid changing them so that they can be set
1368 ;; to make `forward-paragraph' and friends do something the user wants.
1370 ;; `paragraph-start': The `(' in the character alternative and the
1371 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1372 ;; sexps and backquoted sexps that follow a docstring from being filled
1373 ;; with the docstring. This setting has the consequence of inhibiting
1374 ;; filling many program lines that are not docstrings, which is sensible,
1375 ;; because the user probably asked to fill program lines by accident, or
1376 ;; expecting indentation (perhaps we should try to do indenting in that
1377 ;; case). The `;' and `:' stop the paragraph being filled at following
1378 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1379 ;; escaped to keep font-locking, filling, & paren matching in the source
1382 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1383 ;; a docstring and identifies it as a paragraph separator, so that it
1384 ;; won't be filled. (Since the first line of documentation stands alone
1385 ;; in some contexts, filling should not alter the contents the author has
1386 ;; chosen.) Only the first line of a docstring begins with whitespace
1387 ;; and a quotation mark and ends with a period or (rarely) a comma.
1389 ;; The `fill-column' is temporarily bound to
1390 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1391 (let ((paragraph-start (concat paragraph-start
1392 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1394 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1395 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1396 (derived-mode-p 'emacs-lisp-mode))
1397 emacs-lisp-docstring-fill-column
1399 (fill-paragraph justify))
1400 ;; Never return nil.
1403 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1404 "Indent all lines of code, starting in the region, sideways by ARG columns.
1405 Does not affect lines starting inside comments or strings, assuming that
1406 the start of the region is not inside them.
1408 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1409 The last is a regexp which, if matched at the beginning of a line,
1410 means don't indent that line."
1411 (interactive "r\np")
1415 (setq end (point-marker))
1418 (setq state (parse-partial-sexp (point)
1420 (forward-line 1) (point))
1422 (while (< (point) end)
1423 (or (car (nthcdr 3 state))
1424 (and nochange-regexp
1425 (looking-at nochange-regexp))
1426 ;; If line does not start in string, indent it
1427 (let ((indent (current-indentation)))
1428 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1430 (indent-to (max 0 (+ indent arg)) 0))))
1431 (setq state (parse-partial-sexp (point)
1433 (forward-line 1) (point))
1436 (provide 'lisp-mode)
1438 ;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf
1439 ;;; lisp-mode.el ends here