* test/automated/Makefile.in (all): Fix typo.
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blob95eb8c963be99bbcc99279d4e69d10dfb86213c7
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands
3 ;; Copyright (C) 1985-1986, 1999-2012 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: lisp, languages
7 ;; Package: emacs
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/>.
24 ;;; Commentary:
26 ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
27 ;; This mode is documented in the Emacs manual.
29 ;;; Code:
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)
37 (define-abbrev-table 'lisp-mode-abbrev-table ()
38 "Abbrev table for Lisp mode.")
40 (defvar emacs-lisp-mode-abbrev-table nil)
41 (define-abbrev-table 'emacs-lisp-mode-abbrev-table ()
42 "Abbrev table for Emacs Lisp mode.
43 It has `lisp-mode-abbrev-table' as its parent."
44 :parents (list lisp-mode-abbrev-table))
46 (defvar emacs-lisp-mode-syntax-table
47 (let ((table (make-syntax-table))
48 (i 0))
49 (while (< i ?0)
50 (modify-syntax-entry i "_ " table)
51 (setq i (1+ i)))
52 (setq i (1+ ?9))
53 (while (< i ?A)
54 (modify-syntax-entry i "_ " table)
55 (setq i (1+ i)))
56 (setq i (1+ ?Z))
57 (while (< i ?a)
58 (modify-syntax-entry i "_ " table)
59 (setq i (1+ i)))
60 (setq i (1+ ?z))
61 (while (< i 128)
62 (modify-syntax-entry i "_ " table)
63 (setq i (1+ i)))
64 (modify-syntax-entry ?\s " " table)
65 ;; Non-break space acts as whitespace.
66 (modify-syntax-entry ?\x8a0 " " table)
67 (modify-syntax-entry ?\t " " table)
68 (modify-syntax-entry ?\f " " table)
69 (modify-syntax-entry ?\n "> " table)
70 ;; This is probably obsolete since nowadays such features use overlays.
71 ;; ;; Give CR the same syntax as newline, for selective-display.
72 ;; (modify-syntax-entry ?\^m "> " table)
73 (modify-syntax-entry ?\; "< " table)
74 (modify-syntax-entry ?` "' " table)
75 (modify-syntax-entry ?' "' " table)
76 (modify-syntax-entry ?, "' " table)
77 (modify-syntax-entry ?@ "' " table)
78 ;; Used to be singlequote; changed for flonums.
79 (modify-syntax-entry ?. "_ " table)
80 (modify-syntax-entry ?# "' " table)
81 (modify-syntax-entry ?\" "\" " table)
82 (modify-syntax-entry ?\\ "\\ " table)
83 (modify-syntax-entry ?\( "() " table)
84 (modify-syntax-entry ?\) ")( " table)
85 (modify-syntax-entry ?\[ "(] " table)
86 (modify-syntax-entry ?\] ")[ " table)
87 table)
88 "Syntax table used in `emacs-lisp-mode'.")
90 (defvar lisp-mode-syntax-table
91 (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
92 (modify-syntax-entry ?\[ "_ " table)
93 (modify-syntax-entry ?\] "_ " table)
94 (modify-syntax-entry ?# "' 14" table)
95 (modify-syntax-entry ?| "\" 23bn" table)
96 table)
97 "Syntax table used in `lisp-mode'.")
99 (defvar lisp-imenu-generic-expression
100 (list
101 (list nil
102 (purecopy (concat "^\\s-*("
103 (eval-when-compile
104 (regexp-opt
105 '("defun" "defun*" "defsubst" "defmacro"
106 "defadvice" "define-skeleton"
107 "define-minor-mode" "define-global-minor-mode"
108 "define-globalized-minor-mode"
109 "define-derived-mode" "define-generic-mode"
110 "define-compiler-macro" "define-modify-macro"
111 "defsetf" "define-setf-expander"
112 "define-method-combination"
113 "defgeneric" "defmethod") t))
114 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
116 (list (purecopy "Variables")
117 (purecopy (concat "^\\s-*("
118 (eval-when-compile
119 (regexp-opt
120 '("defvar" "defconst" "defconstant" "defcustom"
121 "defparameter" "define-symbol-macro") t))
122 "\\s-+\\(\\(\\sw\\|\\s_\\)+\\)"))
124 (list (purecopy "Types")
125 (purecopy (concat "^\\s-*("
126 (eval-when-compile
127 (regexp-opt
128 '("defgroup" "deftheme" "deftype" "defstruct"
129 "defclass" "define-condition" "define-widget"
130 "defface" "defpackage") t))
131 "\\s-+'?\\(\\(\\sw\\|\\s_\\)+\\)"))
134 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
136 ;; This was originally in autoload.el and is still used there.
137 (put 'autoload 'doc-string-elt 3)
138 (put 'defun 'doc-string-elt 3)
139 (put 'defun* 'doc-string-elt 3)
140 (put 'defmethod 'doc-string-elt 3)
141 (put 'defvar 'doc-string-elt 3)
142 (put 'defcustom 'doc-string-elt 3)
143 (put 'deftheme 'doc-string-elt 2)
144 (put 'deftype 'doc-string-elt 3)
145 (put 'defconst 'doc-string-elt 3)
146 (put 'defmacro 'doc-string-elt 3)
147 (put 'defmacro* 'doc-string-elt 3)
148 (put 'defsubst 'doc-string-elt 3)
149 (put 'defstruct 'doc-string-elt 2)
150 (put 'define-skeleton 'doc-string-elt 2)
151 (put 'define-derived-mode 'doc-string-elt 4)
152 (put 'define-compilation-mode 'doc-string-elt 3)
153 (put 'easy-mmode-define-minor-mode 'doc-string-elt 2)
154 (put 'define-minor-mode 'doc-string-elt 2)
155 (put 'easy-mmode-define-global-mode 'doc-string-elt 2)
156 (put 'define-global-minor-mode 'doc-string-elt 2)
157 (put 'define-globalized-minor-mode 'doc-string-elt 2)
158 (put 'define-generic-mode 'doc-string-elt 7)
159 (put 'define-ibuffer-filter 'doc-string-elt 2)
160 (put 'define-ibuffer-op 'doc-string-elt 3)
161 (put 'define-ibuffer-sorter 'doc-string-elt 2)
162 (put 'lambda 'doc-string-elt 2)
163 (put 'defalias 'doc-string-elt 3)
164 (put 'defvaralias 'doc-string-elt 3)
165 (put 'define-category 'doc-string-elt 2)
166 (put 'define-overloadable-function 'doc-string-elt 3)
168 (defvar lisp-doc-string-elt-property 'doc-string-elt
169 "The symbol property that holds the docstring position info.")
171 (defun lisp-font-lock-syntactic-face-function (state)
172 (if (nth 3 state)
173 ;; This might be a (doc)string or a |...| symbol.
174 (let ((startpos (nth 8 state)))
175 (if (eq (char-after startpos) ?|)
176 ;; This is not a string, but a |...| symbol.
178 (let* ((listbeg (nth 1 state))
179 (firstsym (and listbeg
180 (save-excursion
181 (goto-char listbeg)
182 (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
183 (match-string 1)))))
184 (docelt (and firstsym (get (intern-soft firstsym)
185 lisp-doc-string-elt-property))))
186 (if (and docelt
187 ;; It's a string in a form that can have a docstring.
188 ;; Check whether it's in docstring position.
189 (save-excursion
190 (when (functionp docelt)
191 (goto-char (match-end 1))
192 (setq docelt (funcall docelt)))
193 (goto-char listbeg)
194 (forward-char 1)
195 (condition-case nil
196 (while (and (> docelt 0) (< (point) startpos)
197 (progn (forward-sexp 1) t))
198 (setq docelt (1- docelt)))
199 (error nil))
200 (and (zerop docelt) (<= (point) startpos)
201 (progn (forward-comment (point-max)) t)
202 (= (point) (nth 8 state)))))
203 font-lock-doc-face
204 font-lock-string-face))))
205 font-lock-comment-face))
207 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive)
208 "Common initialization routine for lisp modes.
209 The LISP-SYNTAX argument is used by code in inf-lisp.el and is
210 \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
211 score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for
212 font-lock keywords will not be case sensitive."
213 (when lisp-syntax
214 (set-syntax-table lisp-mode-syntax-table))
215 (make-local-variable 'paragraph-ignore-fill-prefix)
216 (setq paragraph-ignore-fill-prefix t)
217 (make-local-variable 'fill-paragraph-function)
218 (setq fill-paragraph-function 'lisp-fill-paragraph)
219 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
220 ;; a single docstring. Let's fix it here.
221 (set (make-local-variable 'adaptive-fill-function)
222 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
223 ;; Adaptive fill mode gets in the way of auto-fill,
224 ;; and should make no difference for explicit fill
225 ;; because lisp-fill-paragraph should do the job.
226 ;; I believe that newcomment's auto-fill code properly deals with it -stef
227 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
228 (make-local-variable 'indent-line-function)
229 (setq indent-line-function 'lisp-indent-line)
230 (make-local-variable 'outline-regexp)
231 (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
232 (make-local-variable 'outline-level)
233 (setq outline-level 'lisp-outline-level)
234 (make-local-variable 'comment-start)
235 (setq comment-start ";")
236 (make-local-variable 'comment-start-skip)
237 ;; Look within the line for a ; following an even number of backslashes
238 ;; after either a non-backslash or the line beginning.
239 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
240 (make-local-variable 'font-lock-comment-start-skip)
241 ;; Font lock mode uses this only when it KNOWS a comment is starting.
242 (setq font-lock-comment-start-skip ";+ *")
243 (make-local-variable 'comment-add)
244 (setq comment-add 1) ;default to `;;' in comment-region
245 (make-local-variable 'comment-column)
246 (setq comment-column 40)
247 ;; Don't get confused by `;' in doc strings when paragraph-filling.
248 (set (make-local-variable 'comment-use-global-state) t)
249 (make-local-variable 'imenu-generic-expression)
250 (setq imenu-generic-expression lisp-imenu-generic-expression)
251 (make-local-variable 'multibyte-syntax-as-symbol)
252 (setq multibyte-syntax-as-symbol t)
253 (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun)
254 (setq font-lock-defaults
255 `((lisp-font-lock-keywords
256 lisp-font-lock-keywords-1 lisp-font-lock-keywords-2)
257 nil ,keywords-case-insensitive (("+-*/.<>=!?$%_&~^:@" . "w")) nil
258 (font-lock-mark-block-function . mark-defun)
259 (font-lock-syntactic-face-function
260 . lisp-font-lock-syntactic-face-function))))
262 (defun lisp-outline-level ()
263 "Lisp mode `outline-level' function."
264 (let ((len (- (match-end 0) (match-beginning 0))))
265 (if (looking-at "(\\|;;;###autoload")
266 1000
267 len)))
269 (defvar lisp-mode-shared-map
270 (let ((map (make-sparse-keymap)))
271 (define-key map "\e\C-q" 'indent-sexp)
272 (define-key map "\177" 'backward-delete-char-untabify)
273 ;; This gets in the way when viewing a Lisp file in view-mode. As
274 ;; long as [backspace] is mapped into DEL via the
275 ;; function-key-map, this should remain disabled!!
276 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
277 map)
278 "Keymap for commands shared by all sorts of Lisp modes.")
280 (defvar emacs-lisp-mode-map
281 (let ((map (make-sparse-keymap "Emacs-Lisp"))
282 (menu-map (make-sparse-keymap "Emacs-Lisp"))
283 (lint-map (make-sparse-keymap))
284 (prof-map (make-sparse-keymap))
285 (tracing-map (make-sparse-keymap)))
286 (set-keymap-parent map lisp-mode-shared-map)
287 (define-key map "\e\t" 'completion-at-point)
288 (define-key map "\e\C-x" 'eval-defun)
289 (define-key map "\e\C-q" 'indent-pp-sexp)
290 (define-key map [menu-bar emacs-lisp] (cons (purecopy "Emacs-Lisp") menu-map))
291 (define-key menu-map [eldoc]
292 `(menu-item ,(purecopy "Auto-Display Documentation Strings") eldoc-mode
293 :button (:toggle . (bound-and-true-p eldoc-mode))
294 :help ,(purecopy "Display the documentation string for the item under cursor")))
295 (define-key menu-map [checkdoc]
296 `(menu-item ,(purecopy "Check Documentation Strings") checkdoc
297 :help ,(purecopy "Check documentation strings for style requirements")))
298 (define-key menu-map [re-builder]
299 `(menu-item ,(purecopy "Construct Regexp") re-builder
300 :help ,(purecopy "Construct a regexp interactively")))
301 (define-key menu-map [tracing] (cons (purecopy "Tracing") tracing-map))
302 (define-key tracing-map [tr-a]
303 `(menu-item ,(purecopy "Untrace All") untrace-all
304 :help ,(purecopy "Untrace all currently traced functions")))
305 (define-key tracing-map [tr-uf]
306 `(menu-item ,(purecopy "Untrace Function...") untrace-function
307 :help ,(purecopy "Untrace function, and possibly activate all remaining advice")))
308 (define-key tracing-map [tr-sep] menu-bar-separator)
309 (define-key tracing-map [tr-q]
310 `(menu-item ,(purecopy "Trace Function Quietly...") trace-function-background
311 :help ,(purecopy "Trace the function with trace output going quietly to a buffer")))
312 (define-key tracing-map [tr-f]
313 `(menu-item ,(purecopy "Trace Function...") trace-function
314 :help ,(purecopy "Trace the function given as an argument")))
315 (define-key menu-map [profiling] (cons (purecopy "Profiling") prof-map))
316 (define-key prof-map [prof-restall]
317 `(menu-item ,(purecopy "Remove Instrumentation for All Functions") elp-restore-all
318 :help ,(purecopy "Restore the original definitions of all functions being profiled")))
319 (define-key prof-map [prof-restfunc]
320 `(menu-item ,(purecopy "Remove Instrumentation for Function...") elp-restore-function
321 :help ,(purecopy "Restore an instrumented function to its original definition")))
323 (define-key prof-map [sep-rem] menu-bar-separator)
324 (define-key prof-map [prof-resall]
325 `(menu-item ,(purecopy "Reset Counters for All Functions") elp-reset-all
326 :help ,(purecopy "Reset the profiling information for all functions being profiled")))
327 (define-key prof-map [prof-resfunc]
328 `(menu-item ,(purecopy "Reset Counters for Function...") elp-reset-function
329 :help ,(purecopy "Reset the profiling information for a function")))
330 (define-key prof-map [prof-res]
331 `(menu-item ,(purecopy "Show Profiling Results") elp-results
332 :help ,(purecopy "Display current profiling results")))
333 (define-key prof-map [prof-pack]
334 `(menu-item ,(purecopy "Instrument Package...") elp-instrument-package
335 :help ,(purecopy "Instrument for profiling all function that start with a prefix")))
336 (define-key prof-map [prof-func]
337 `(menu-item ,(purecopy "Instrument Function...") elp-instrument-function
338 :help ,(purecopy "Instrument a function for profiling")))
339 (define-key menu-map [lint] (cons (purecopy "Linting") lint-map))
340 (define-key lint-map [lint-di]
341 `(menu-item ,(purecopy "Lint Directory...") elint-directory
342 :help ,(purecopy "Lint a directory")))
343 (define-key lint-map [lint-f]
344 `(menu-item ,(purecopy "Lint File...") elint-file
345 :help ,(purecopy "Lint a file")))
346 (define-key lint-map [lint-b]
347 `(menu-item ,(purecopy "Lint Buffer") elint-current-buffer
348 :help ,(purecopy "Lint the current buffer")))
349 (define-key lint-map [lint-d]
350 `(menu-item ,(purecopy "Lint Defun") elint-defun
351 :help ,(purecopy "Lint the function at point")))
352 (define-key menu-map [edebug-defun]
353 `(menu-item ,(purecopy "Instrument Function for Debugging") edebug-defun
354 :help ,(purecopy "Evaluate the top level form point is in, stepping through with Edebug")
355 :keys ,(purecopy "C-u C-M-x")))
356 (define-key menu-map [separator-byte] menu-bar-separator)
357 (define-key menu-map [disas]
358 `(menu-item ,(purecopy "Disassemble Byte Compiled Object...") disassemble
359 :help ,(purecopy "Print disassembled code for OBJECT in a buffer")))
360 (define-key menu-map [byte-recompile]
361 `(menu-item ,(purecopy "Byte-recompile Directory...") byte-recompile-directory
362 :help ,(purecopy "Recompile every `.el' file in DIRECTORY that needs recompilation")))
363 (define-key menu-map [emacs-byte-compile-and-load]
364 `(menu-item ,(purecopy "Byte-compile and Load") emacs-lisp-byte-compile-and-load
365 :help ,(purecopy "Byte-compile the current file (if it has changed), then load compiled code")))
366 (define-key menu-map [byte-compile]
367 `(menu-item ,(purecopy "Byte-compile This File") emacs-lisp-byte-compile
368 :help ,(purecopy "Byte compile the file containing the current buffer")))
369 (define-key menu-map [separator-eval] menu-bar-separator)
370 (define-key menu-map [ielm]
371 `(menu-item ,(purecopy "Interactive Expression Evaluation") ielm
372 :help ,(purecopy "Interactively evaluate Emacs Lisp expressions")))
373 (define-key menu-map [eval-buffer]
374 `(menu-item ,(purecopy "Evaluate Buffer") eval-buffer
375 :help ,(purecopy "Execute the current buffer as Lisp code")))
376 (define-key menu-map [eval-region]
377 `(menu-item ,(purecopy "Evaluate Region") eval-region
378 :help ,(purecopy "Execute the region as Lisp code")
379 :enable mark-active))
380 (define-key menu-map [eval-sexp]
381 `(menu-item ,(purecopy "Evaluate Last S-expression") eval-last-sexp
382 :help ,(purecopy "Evaluate sexp before point; print value in minibuffer")))
383 (define-key menu-map [separator-format] menu-bar-separator)
384 (define-key menu-map [comment-region]
385 `(menu-item ,(purecopy "Comment Out Region") comment-region
386 :help ,(purecopy "Comment or uncomment each line in the region")
387 :enable mark-active))
388 (define-key menu-map [indent-region]
389 `(menu-item ,(purecopy "Indent Region") indent-region
390 :help ,(purecopy "Indent each nonblank line in the region")
391 :enable mark-active))
392 (define-key menu-map [indent-line]
393 `(menu-item ,(purecopy "Indent Line") lisp-indent-line))
394 map)
395 "Keymap for Emacs Lisp mode.
396 All commands in `lisp-mode-shared-map' are inherited by this map.")
398 (defun emacs-lisp-byte-compile ()
399 "Byte compile the file containing the current buffer."
400 (interactive)
401 (if buffer-file-name
402 (byte-compile-file buffer-file-name)
403 (error "The buffer must be saved in a file first")))
405 (defun emacs-lisp-byte-compile-and-load ()
406 "Byte-compile the current file (if it has changed), then load compiled code."
407 (interactive)
408 (or buffer-file-name
409 (error "The buffer must be saved in a file first"))
410 (require 'bytecomp)
411 ;; Recompile if file or buffer has changed since last compilation.
412 (if (and (buffer-modified-p)
413 (y-or-n-p (format "Save buffer %s first? " (buffer-name))))
414 (save-buffer))
415 (byte-recompile-file buffer-file-name nil 0 t))
417 (defcustom emacs-lisp-mode-hook nil
418 "Hook run when entering Emacs Lisp mode."
419 :options '(turn-on-eldoc-mode imenu-add-menubar-index checkdoc-minor-mode)
420 :type 'hook
421 :group 'lisp)
423 (defcustom lisp-mode-hook nil
424 "Hook run when entering Lisp mode."
425 :options '(imenu-add-menubar-index)
426 :type 'hook
427 :group 'lisp)
429 (defcustom lisp-interaction-mode-hook nil
430 "Hook run when entering Lisp Interaction mode."
431 :options '(turn-on-eldoc-mode)
432 :type 'hook
433 :group 'lisp)
435 (define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp"
436 "Major mode for editing Lisp code to run in Emacs.
437 Commands:
438 Delete converts tabs to spaces as it moves back.
439 Blank lines separate paragraphs. Semicolons start comments.
441 \\{emacs-lisp-mode-map}
442 Entry to this mode calls the value of `emacs-lisp-mode-hook'
443 if that value is non-nil."
444 :group 'lisp
445 (lisp-mode-variables)
446 (setq imenu-case-fold-search nil)
447 (add-hook 'completion-at-point-functions
448 'lisp-completion-at-point nil 'local))
450 (defvar lisp-mode-map
451 (let ((map (make-sparse-keymap))
452 (menu-map (make-sparse-keymap "Lisp")))
453 (set-keymap-parent map lisp-mode-shared-map)
454 (define-key map "\e\C-x" 'lisp-eval-defun)
455 (define-key map "\C-c\C-z" 'run-lisp)
456 (define-key map [menu-bar lisp] (cons (purecopy "Lisp") menu-map))
457 (define-key menu-map [run-lisp]
458 `(menu-item ,(purecopy "Run inferior Lisp") run-lisp
459 :help ,(purecopy "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'")))
460 (define-key menu-map [ev-def]
461 `(menu-item ,(purecopy "Eval defun") lisp-eval-defun
462 :help ,(purecopy "Send the current defun to the Lisp process made by M-x run-lisp")))
463 (define-key menu-map [ind-sexp]
464 `(menu-item ,(purecopy "Indent sexp") indent-sexp
465 :help ,(purecopy "Indent each line of the list starting just after point")))
466 map)
467 "Keymap for ordinary Lisp mode.
468 All commands in `lisp-mode-shared-map' are inherited by this map.")
470 (define-derived-mode lisp-mode prog-mode "Lisp"
471 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
472 Commands:
473 Delete converts tabs to spaces as it moves back.
474 Blank lines separate paragraphs. Semicolons start comments.
476 \\{lisp-mode-map}
477 Note that `run-lisp' may be used either to start an inferior Lisp job
478 or to switch back to an existing one.
480 Entry to this mode calls the value of `lisp-mode-hook'
481 if that value is non-nil."
482 (lisp-mode-variables nil t)
483 (set (make-local-variable 'find-tag-default-function) 'lisp-find-tag-default)
484 (make-local-variable 'comment-start-skip)
485 (setq comment-start-skip
486 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
487 (setq imenu-case-fold-search t))
489 (defun lisp-find-tag-default ()
490 (let ((default (find-tag-default)))
491 (when (stringp default)
492 (if (string-match ":+" default)
493 (substring default (match-end 0))
494 default))))
496 ;; Used in old LispM code.
497 (defalias 'common-lisp-mode 'lisp-mode)
499 ;; This will do unless inf-lisp.el is loaded.
500 (defun lisp-eval-defun (&optional and-go)
501 "Send the current defun to the Lisp process made by \\[run-lisp]."
502 (interactive)
503 (error "Process lisp does not exist"))
505 (defvar lisp-interaction-mode-map
506 (let ((map (make-sparse-keymap))
507 (menu-map (make-sparse-keymap "Lisp-Interaction")))
508 (set-keymap-parent map lisp-mode-shared-map)
509 (define-key map "\e\C-x" 'eval-defun)
510 (define-key map "\e\C-q" 'indent-pp-sexp)
511 (define-key map "\e\t" 'completion-at-point)
512 (define-key map "\n" 'eval-print-last-sexp)
513 (define-key map [menu-bar lisp-interaction] (cons (purecopy "Lisp-Interaction") menu-map))
514 (define-key menu-map [eval-defun]
515 `(menu-item ,(purecopy "Evaluate Defun") eval-defun
516 :help ,(purecopy "Evaluate the top-level form containing point, or after point")))
517 (define-key menu-map [eval-print-last-sexp]
518 `(menu-item ,(purecopy "Evaluate and Print") eval-print-last-sexp
519 :help ,(purecopy "Evaluate sexp before point; print value into current buffer")))
520 (define-key menu-map [edebug-defun-lisp-interaction]
521 `(menu-item ,(purecopy "Instrument Function for Debugging") edebug-defun
522 :help ,(purecopy "Evaluate the top level form point is in, stepping through with Edebug")
523 :keys ,(purecopy "C-u C-M-x")))
524 (define-key menu-map [indent-pp-sexp]
525 `(menu-item ,(purecopy "Indent or Pretty-Print") indent-pp-sexp
526 :help ,(purecopy "Indent each line of the list starting just after point, or prettyprint it")))
527 (define-key menu-map [complete-symbol]
528 `(menu-item ,(purecopy "Complete Lisp Symbol") completion-at-point
529 :help ,(purecopy "Perform completion on Lisp symbol preceding point")))
530 map)
531 "Keymap for Lisp Interaction mode.
532 All commands in `lisp-mode-shared-map' are inherited by this map.")
534 (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction"
535 "Major mode for typing and evaluating Lisp forms.
536 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
537 before point, and prints its value into the buffer, advancing point.
538 Note that printing is controlled by `eval-expression-print-length'
539 and `eval-expression-print-level'.
541 Commands:
542 Delete converts tabs to spaces as it moves back.
543 Paragraphs are separated only by blank lines.
544 Semicolons start comments.
546 \\{lisp-interaction-mode-map}
547 Entry to this mode calls the value of `lisp-interaction-mode-hook'
548 if that value is non-nil."
549 :abbrev-table nil)
551 (defun eval-print-last-sexp ()
552 "Evaluate sexp before point; print value into current buffer.
554 If `eval-expression-debug-on-error' is non-nil, which is the default,
555 this command arranges for all errors to enter the debugger.
557 Note that printing the result is controlled by the variables
558 `eval-expression-print-length' and `eval-expression-print-level',
559 which see."
560 (interactive)
561 (let ((standard-output (current-buffer)))
562 (terpri)
563 (eval-last-sexp t)
564 (terpri)))
567 (defun last-sexp-setup-props (beg end value alt1 alt2)
568 "Set up text properties for the output of `eval-last-sexp-1'.
569 BEG and END are the start and end of the output in current-buffer.
570 VALUE is the Lisp value printed, ALT1 and ALT2 are strings for the
571 alternative printed representations that can be displayed."
572 (let ((map (make-sparse-keymap)))
573 (define-key map "\C-m" 'last-sexp-toggle-display)
574 (define-key map [down-mouse-2] 'mouse-set-point)
575 (define-key map [mouse-2] 'last-sexp-toggle-display)
576 (add-text-properties
577 beg end
578 `(printed-value (,value ,alt1 ,alt2)
579 mouse-face highlight
580 keymap ,map
581 help-echo "RET, mouse-2: toggle abbreviated display"
582 rear-nonsticky (mouse-face keymap help-echo
583 printed-value)))))
586 (defun last-sexp-toggle-display (&optional arg)
587 "Toggle between abbreviated and unabbreviated printed representations."
588 (interactive "P")
589 (save-restriction
590 (widen)
591 (let ((value (get-text-property (point) 'printed-value)))
592 (when value
593 (let ((beg (or (previous-single-property-change (min (point-max) (1+ (point)))
594 'printed-value)
595 (point)))
596 (end (or (next-single-char-property-change (point) 'printed-value) (point)))
597 (standard-output (current-buffer))
598 (point (point)))
599 (delete-region beg end)
600 (insert (nth 1 value))
601 (or (= beg point)
602 (setq point (1- (point))))
603 (last-sexp-setup-props beg (point)
604 (nth 0 value)
605 (nth 2 value)
606 (nth 1 value))
607 (goto-char (min (point-max) point)))))))
609 (defun prin1-char (char)
610 "Return a string representing CHAR as a character rather than as an integer.
611 If CHAR is not a character, return nil."
612 (and (integerp char)
613 (eventp char)
614 (let ((c (event-basic-type char))
615 (mods (event-modifiers char))
616 string)
617 ;; Prevent ?A from turning into ?\S-a.
618 (if (and (memq 'shift mods)
619 (zerop (logand char ?\S-\^@))
620 (not (let ((case-fold-search nil))
621 (char-equal c (upcase c)))))
622 (setq c (upcase c) mods nil))
623 ;; What string are we considering using?
624 (condition-case nil
625 (setq string
626 (concat
628 (mapconcat
629 (lambda (modif)
630 (cond ((eq modif 'super) "\\s-")
631 (t (string ?\\ (upcase (aref (symbol-name modif) 0)) ?-))))
632 mods "")
633 (cond
634 ((memq c '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\)) (string ?\\ c))
635 ((eq c 127) "\\C-?")
637 (string c)))))
638 (error nil))
639 ;; Verify the string reads a CHAR, not to some other character.
640 ;; If it doesn't, return nil instead.
641 (and string
642 (= (car (read-from-string string)) char)
643 string))))
646 (defun preceding-sexp ()
647 "Return sexp before the point."
648 (let ((opoint (point))
649 ignore-quotes
650 expr)
651 (save-excursion
652 (with-syntax-table emacs-lisp-mode-syntax-table
653 ;; If this sexp appears to be enclosed in `...'
654 ;; then ignore the surrounding quotes.
655 (setq ignore-quotes
656 (or (eq (following-char) ?\')
657 (eq (preceding-char) ?\')))
658 (forward-sexp -1)
659 ;; If we were after `?\e' (or similar case),
660 ;; use the whole thing, not just the `e'.
661 (when (eq (preceding-char) ?\\)
662 (forward-char -1)
663 (when (eq (preceding-char) ??)
664 (forward-char -1)))
666 ;; Skip over hash table read syntax.
667 (and (> (point) (1+ (point-min)))
668 (looking-back "#s" (- (point) 2))
669 (forward-char -2))
671 ;; Skip over `#N='s.
672 (when (eq (preceding-char) ?=)
673 (let (labeled-p)
674 (save-excursion
675 (skip-chars-backward "0-9#=")
676 (setq labeled-p (looking-at "\\(#[0-9]+=\\)+")))
677 (when labeled-p
678 (forward-sexp -1))))
680 (save-restriction
681 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in
682 ;; `variable' so that the value is returned, not the
683 ;; name
684 (if (and ignore-quotes
685 (eq (following-char) ?`))
686 (forward-char))
687 (narrow-to-region (point-min) opoint)
688 (setq expr (read (current-buffer)))
689 ;; If it's an (interactive ...) form, it's more
690 ;; useful to show how an interactive call would
691 ;; use it.
692 (and (consp expr)
693 (eq (car expr) 'interactive)
694 (setq expr
695 (list 'call-interactively
696 (list 'quote
697 (list 'lambda
698 '(&rest args)
699 expr
700 'args)))))
701 expr)))))
704 (defun eval-last-sexp-1 (eval-last-sexp-arg-internal)
705 "Evaluate sexp before point; print value in minibuffer.
706 With argument, print output into current buffer."
707 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)))
708 ;; Setup the lexical environment if lexical-binding is enabled.
709 (eval-last-sexp-print-value
710 (eval (eval-sexp-add-defvars (preceding-sexp)) lexical-binding))))
713 (defun eval-last-sexp-print-value (value)
714 (let ((unabbreviated (let ((print-length nil) (print-level nil))
715 (prin1-to-string value)))
716 (print-length eval-expression-print-length)
717 (print-level eval-expression-print-level)
718 (beg (point))
719 end)
720 (prog1
721 (prin1 value)
722 (let ((str (eval-expression-print-format value)))
723 (if str (princ str)))
724 (setq end (point))
725 (when (and (bufferp standard-output)
726 (or (not (null print-length))
727 (not (null print-level)))
728 (not (string= unabbreviated
729 (buffer-substring-no-properties beg end))))
730 (last-sexp-setup-props beg end value
731 unabbreviated
732 (buffer-substring-no-properties beg end))
733 ))))
736 (defvar eval-last-sexp-fake-value (make-symbol "t"))
738 (defun eval-sexp-add-defvars (exp &optional pos)
739 "Prepend EXP with all the `defvar's that precede it in the buffer.
740 POS specifies the starting position where EXP was found and defaults to point."
741 (if (not lexical-binding)
743 (save-excursion
744 (unless pos (setq pos (point)))
745 (let ((vars ()))
746 (goto-char (point-min))
747 (while (re-search-forward
748 "^(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)"
749 pos t)
750 (let ((var (intern (match-string 1))))
751 (unless (special-variable-p var)
752 (push var vars))))
753 `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp)))))
755 (defun eval-last-sexp (eval-last-sexp-arg-internal)
756 "Evaluate sexp before point; print value in minibuffer.
757 Interactively, with prefix argument, print output into current buffer.
758 Truncates long output according to the value of the variables
759 `eval-expression-print-length' and `eval-expression-print-level'.
761 If `eval-expression-debug-on-error' is non-nil, which is the default,
762 this command arranges for all errors to enter the debugger."
763 (interactive "P")
764 (if (null eval-expression-debug-on-error)
765 (eval-last-sexp-1 eval-last-sexp-arg-internal)
766 (let ((value
767 (let ((debug-on-error eval-last-sexp-fake-value))
768 (cons (eval-last-sexp-1 eval-last-sexp-arg-internal)
769 debug-on-error))))
770 (unless (eq (cdr value) eval-last-sexp-fake-value)
771 (setq debug-on-error (cdr value)))
772 (car value))))
774 (defun eval-defun-1 (form)
775 "Treat some expressions specially.
776 Reset the `defvar' and `defcustom' variables to the initial value.
777 Reinitialize the face according to the `defface' specification."
778 ;; The code in edebug-defun should be consistent with this, but not
779 ;; the same, since this gets a macroexpanded form.
780 (cond ((not (listp form))
781 form)
782 ((and (eq (car form) 'defvar)
783 (cdr-safe (cdr-safe form))
784 (boundp (cadr form)))
785 ;; Force variable to be re-set.
786 `(progn (defvar ,(nth 1 form) nil ,@(nthcdr 3 form))
787 (setq-default ,(nth 1 form) ,(nth 2 form))))
788 ;; `defcustom' is now macroexpanded to
789 ;; `custom-declare-variable' with a quoted value arg.
790 ((and (eq (car form) 'custom-declare-variable)
791 (default-boundp (eval (nth 1 form) lexical-binding)))
792 ;; Force variable to be bound.
793 (set-default (eval (nth 1 form) lexical-binding)
794 (eval (nth 1 (nth 2 form)) lexical-binding))
795 form)
796 ;; `defface' is macroexpanded to `custom-declare-face'.
797 ((eq (car form) 'custom-declare-face)
798 ;; Reset the face.
799 (let ((face-symbol (eval (nth 1 form) lexical-binding)))
800 (setq face-new-frame-defaults
801 (assq-delete-all face-symbol face-new-frame-defaults))
802 (put face-symbol 'face-defface-spec nil)
803 (put face-symbol 'face-documentation (nth 3 form))
804 ;; Setting `customized-face' to the new spec after calling
805 ;; the form, but preserving the old saved spec in `saved-face',
806 ;; imitates the situation when the new face spec is set
807 ;; temporarily for the current session in the customize
808 ;; buffer, thus allowing `face-user-default-spec' to use the
809 ;; new customized spec instead of the saved spec.
810 ;; Resetting `saved-face' temporarily to nil is needed to let
811 ;; `defface' change the spec, regardless of a saved spec.
812 (prog1 `(prog1 ,form
813 (put ,(nth 1 form) 'saved-face
814 ',(get face-symbol 'saved-face))
815 (put ,(nth 1 form) 'customized-face
816 ,(nth 2 form)))
817 (put face-symbol 'saved-face nil))))
818 ((eq (car form) 'progn)
819 (cons 'progn (mapcar 'eval-defun-1 (cdr form))))
820 (t form)))
822 (defun eval-defun-2 ()
823 "Evaluate defun that point is in or before.
824 The value is displayed in the minibuffer.
825 If the current defun is actually a call to `defvar',
826 then reset the variable using the initial value expression
827 even if the variable already has some other value.
828 \(Normally `defvar' does not change the variable's value
829 if it already has a value.\)
831 With argument, insert value in current buffer after the defun.
832 Return the result of evaluation."
833 (interactive "P")
834 ;; FIXME: the print-length/level bindings should only be applied while
835 ;; printing, not while evaluating.
836 (let ((debug-on-error eval-expression-debug-on-error)
837 (print-length eval-expression-print-length)
838 (print-level eval-expression-print-level))
839 (save-excursion
840 ;; Arrange for eval-region to "read" the (possibly) altered form.
841 ;; eval-region handles recording which file defines a function or
842 ;; variable. Re-written using `apply' to avoid capturing
843 ;; variables like `end'.
844 (apply
845 #'eval-region
846 (let ((standard-output t)
847 beg end form)
848 ;; Read the form from the buffer, and record where it ends.
849 (save-excursion
850 (end-of-defun)
851 (beginning-of-defun)
852 (setq beg (point))
853 (setq form (eval-sexp-add-defvars (read (current-buffer))))
854 (setq end (point)))
855 ;; Alter the form if necessary.
856 (setq form (eval-defun-1 (macroexpand form)))
857 (list beg end standard-output
858 `(lambda (ignore)
859 ;; Skipping to the end of the specified region
860 ;; will make eval-region return.
861 (goto-char ,end)
862 ',form))))))
863 ;; The result of evaluation has been put onto VALUES. So return it.
864 (car values))
866 (defun eval-defun (edebug-it)
867 "Evaluate the top-level form containing point, or after point.
869 If the current defun is actually a call to `defvar' or `defcustom',
870 evaluating it this way resets the variable using its initial value
871 expression even if the variable already has some other value.
872 \(Normally `defvar' and `defcustom' do not alter the value if there
873 already is one.) In an analogous way, evaluating a `defface'
874 overrides any customizations of the face, so that it becomes
875 defined exactly as the `defface' expression says.
877 If `eval-expression-debug-on-error' is non-nil, which is the default,
878 this command arranges for all errors to enter the debugger.
880 With a prefix argument, instrument the code for Edebug.
882 If acting on a `defun' for FUNCTION, and the function was
883 instrumented, `Edebug: FUNCTION' is printed in the minibuffer. If not
884 instrumented, just FUNCTION is printed.
886 If not acting on a `defun', the result of evaluation is displayed in
887 the minibuffer. This display is controlled by the variables
888 `eval-expression-print-length' and `eval-expression-print-level',
889 which see."
890 (interactive "P")
891 (cond (edebug-it
892 (require 'edebug)
893 (eval-defun (not edebug-all-defs)))
895 (if (null eval-expression-debug-on-error)
896 (eval-defun-2)
897 (let ((old-value (make-symbol "t")) new-value value)
898 (let ((debug-on-error old-value))
899 (setq value (eval-defun-2))
900 (setq new-value debug-on-error))
901 (unless (eq old-value new-value)
902 (setq debug-on-error new-value))
903 value)))))
905 ;; May still be used by some external Lisp-mode variant.
906 (define-obsolete-function-alias 'lisp-comment-indent
907 'comment-indent-default "22.1")
908 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
910 (defcustom lisp-indent-offset nil
911 "If non-nil, indent second line of expressions that many more columns."
912 :group 'lisp
913 :type '(choice (const nil) integer))
914 (put 'lisp-indent-offset 'safe-local-variable
915 (lambda (x) (or (null x) (integerp x))))
917 (defcustom lisp-indent-function 'lisp-indent-function
918 "A function to be called by `calculate-lisp-indent'.
919 It indents the arguments of a Lisp function call. This function
920 should accept two arguments: the indent-point, and the
921 `parse-partial-sexp' state at that position. One option for this
922 function is `common-lisp-indent-function'."
923 :type 'function
924 :group 'lisp)
926 (defun lisp-indent-line (&optional whole-exp)
927 "Indent current line as Lisp code.
928 With argument, indent any additional lines of the same expression
929 rigidly along with this one."
930 (interactive "P")
931 (let ((indent (calculate-lisp-indent)) shift-amt end
932 (pos (- (point-max) (point)))
933 (beg (progn (beginning-of-line) (point))))
934 (skip-chars-forward " \t")
935 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
936 ;; Don't alter indentation of a ;;; comment line
937 ;; or a line that starts in a string.
938 (goto-char (- (point-max) pos))
939 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
940 ;; Single-semicolon comment lines should be indented
941 ;; as comment lines, not as code.
942 (progn (indent-for-comment) (forward-char -1))
943 (if (listp indent) (setq indent (car indent)))
944 (setq shift-amt (- indent (current-column)))
945 (if (zerop shift-amt)
947 (delete-region beg (point))
948 (indent-to indent)))
949 ;; If initial point was within line's indentation,
950 ;; position after the indentation. Else stay at same point in text.
951 (if (> (- (point-max) pos) (point))
952 (goto-char (- (point-max) pos)))
953 ;; If desired, shift remaining lines of expression the same amount.
954 (and whole-exp (not (zerop shift-amt))
955 (save-excursion
956 (goto-char beg)
957 (forward-sexp 1)
958 (setq end (point))
959 (goto-char beg)
960 (forward-line 1)
961 (setq beg (point))
962 (> end beg))
963 (indent-code-rigidly beg end shift-amt)))))
965 (defvar calculate-lisp-indent-last-sexp)
967 (defun calculate-lisp-indent (&optional parse-start)
968 "Return appropriate indentation for current line as Lisp code.
969 In usual case returns an integer: the column to indent to.
970 If the value is nil, that means don't change the indentation
971 because the line starts inside a string.
973 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
974 This means that following lines at the same level of indentation
975 should not necessarily be indented the same as this line.
976 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
977 is the buffer position of the start of the containing expression."
978 (save-excursion
979 (beginning-of-line)
980 (let ((indent-point (point))
981 state paren-depth
982 ;; setting this to a number inhibits calling hook
983 (desired-indent nil)
984 (retry t)
985 calculate-lisp-indent-last-sexp containing-sexp)
986 (if parse-start
987 (goto-char parse-start)
988 (beginning-of-defun))
989 ;; Find outermost containing sexp
990 (while (< (point) indent-point)
991 (setq state (parse-partial-sexp (point) indent-point 0)))
992 ;; Find innermost containing sexp
993 (while (and retry
994 state
995 (> (setq paren-depth (elt state 0)) 0))
996 (setq retry nil)
997 (setq calculate-lisp-indent-last-sexp (elt state 2))
998 (setq containing-sexp (elt state 1))
999 ;; Position following last unclosed open.
1000 (goto-char (1+ containing-sexp))
1001 ;; Is there a complete sexp since then?
1002 (if (and calculate-lisp-indent-last-sexp
1003 (> calculate-lisp-indent-last-sexp (point)))
1004 ;; Yes, but is there a containing sexp after that?
1005 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
1006 indent-point 0)))
1007 (if (setq retry (car (cdr peek))) (setq state peek)))))
1008 (if retry
1010 ;; Innermost containing sexp found
1011 (goto-char (1+ containing-sexp))
1012 (if (not calculate-lisp-indent-last-sexp)
1013 ;; indent-point immediately follows open paren.
1014 ;; Don't call hook.
1015 (setq desired-indent (current-column))
1016 ;; Find the start of first element of containing sexp.
1017 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1018 (cond ((looking-at "\\s(")
1019 ;; First element of containing sexp is a list.
1020 ;; Indent under that list.
1022 ((> (save-excursion (forward-line 1) (point))
1023 calculate-lisp-indent-last-sexp)
1024 ;; This is the first line to start within the containing sexp.
1025 ;; It's almost certainly a function call.
1026 (if (= (point) calculate-lisp-indent-last-sexp)
1027 ;; Containing sexp has nothing before this line
1028 ;; except the first element. Indent under that element.
1030 ;; Skip the first element, find start of second (the first
1031 ;; argument of the function call) and indent under.
1032 (progn (forward-sexp 1)
1033 (parse-partial-sexp (point)
1034 calculate-lisp-indent-last-sexp
1035 0 t)))
1036 (backward-prefix-chars))
1038 ;; Indent beneath first sexp on same line as
1039 ;; `calculate-lisp-indent-last-sexp'. Again, it's
1040 ;; almost certainly a function call.
1041 (goto-char calculate-lisp-indent-last-sexp)
1042 (beginning-of-line)
1043 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
1044 0 t)
1045 (backward-prefix-chars)))))
1046 ;; Point is at the point to indent under unless we are inside a string.
1047 ;; Call indentation hook except when overridden by lisp-indent-offset
1048 ;; or if the desired indentation has already been computed.
1049 (let ((normal-indent (current-column)))
1050 (cond ((elt state 3)
1051 ;; Inside a string, don't change indentation.
1052 nil)
1053 ((and (integerp lisp-indent-offset) containing-sexp)
1054 ;; Indent by constant offset
1055 (goto-char containing-sexp)
1056 (+ (current-column) lisp-indent-offset))
1057 ;; in this case calculate-lisp-indent-last-sexp is not nil
1058 (calculate-lisp-indent-last-sexp
1060 ;; try to align the parameters of a known function
1061 (and lisp-indent-function
1062 (not retry)
1063 (funcall lisp-indent-function indent-point state))
1064 ;; If the function has no special alignment
1065 ;; or it does not apply to this argument,
1066 ;; try to align a constant-symbol under the last
1067 ;; preceding constant symbol, if there is such one of
1068 ;; the last 2 preceding symbols, in the previous
1069 ;; uncommented line.
1070 (and (save-excursion
1071 (goto-char indent-point)
1072 (skip-chars-forward " \t")
1073 (looking-at ":"))
1074 ;; The last sexp may not be at the indentation
1075 ;; where it begins, so find that one, instead.
1076 (save-excursion
1077 (goto-char calculate-lisp-indent-last-sexp)
1078 ;; Handle prefix characters and whitespace
1079 ;; following an open paren. (Bug#1012)
1080 (backward-prefix-chars)
1081 (while (and (not (looking-back "^[ \t]*\\|([ \t]+"))
1082 (or (not containing-sexp)
1083 (< (1+ containing-sexp) (point))))
1084 (forward-sexp -1)
1085 (backward-prefix-chars))
1086 (setq calculate-lisp-indent-last-sexp (point)))
1087 (> calculate-lisp-indent-last-sexp
1088 (save-excursion
1089 (goto-char (1+ containing-sexp))
1090 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1091 (point)))
1092 (let ((parse-sexp-ignore-comments t)
1093 indent)
1094 (goto-char calculate-lisp-indent-last-sexp)
1095 (or (and (looking-at ":")
1096 (setq indent (current-column)))
1097 (and (< (line-beginning-position)
1098 (prog2 (backward-sexp) (point)))
1099 (looking-at ":")
1100 (setq indent (current-column))))
1101 indent))
1102 ;; another symbols or constants not preceded by a constant
1103 ;; as defined above.
1104 normal-indent))
1105 ;; in this case calculate-lisp-indent-last-sexp is nil
1106 (desired-indent)
1108 normal-indent))))))
1110 (defun lisp-indent-function (indent-point state)
1111 "This function is the normal value of the variable `lisp-indent-function'.
1112 The function `calculate-lisp-indent' calls this to determine
1113 if the arguments of a Lisp function call should be indented specially.
1115 INDENT-POINT is the position where the user typed TAB, or equivalent.
1116 Point is located at the point to indent under (for default indentation);
1117 STATE is the `parse-partial-sexp' state for that position.
1119 If the current line is in a call to a Lisp function that has a non-nil
1120 property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
1121 it specifies how to indent. The property value can be:
1123 * `defun', meaning indent `defun'-style
1124 \(this is also the case if there is no property and the function
1125 has a name that begins with \"def\", and three or more arguments);
1127 * an integer N, meaning indent the first N arguments specially
1128 (like ordinary function arguments), and then indent any further
1129 arguments like a body;
1131 * a function to call that returns the indentation (or nil).
1132 `lisp-indent-function' calls this function with the same two arguments
1133 that it itself received.
1135 This function returns either the indentation to use, or nil if the
1136 Lisp function does not specify a special indentation."
1137 (let ((normal-indent (current-column)))
1138 (goto-char (1+ (elt state 1)))
1139 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
1140 (if (and (elt state 2)
1141 (not (looking-at "\\sw\\|\\s_")))
1142 ;; car of form doesn't seem to be a symbol
1143 (progn
1144 (if (not (> (save-excursion (forward-line 1) (point))
1145 calculate-lisp-indent-last-sexp))
1146 (progn (goto-char calculate-lisp-indent-last-sexp)
1147 (beginning-of-line)
1148 (parse-partial-sexp (point)
1149 calculate-lisp-indent-last-sexp 0 t)))
1150 ;; Indent under the list or under the first sexp on the same
1151 ;; line as calculate-lisp-indent-last-sexp. Note that first
1152 ;; thing on that line has to be complete sexp since we are
1153 ;; inside the innermost containing sexp.
1154 (backward-prefix-chars)
1155 (current-column))
1156 (let ((function (buffer-substring (point)
1157 (progn (forward-sexp 1) (point))))
1158 method)
1159 (setq method (or (get (intern-soft function) 'lisp-indent-function)
1160 (get (intern-soft function) 'lisp-indent-hook)))
1161 (cond ((or (eq method 'defun)
1162 (and (null method)
1163 (> (length function) 3)
1164 (string-match "\\`def" function)))
1165 (lisp-indent-defform state indent-point))
1166 ((integerp method)
1167 (lisp-indent-specform method state
1168 indent-point normal-indent))
1169 (method
1170 (funcall method indent-point state)))))))
1172 (defcustom lisp-body-indent 2
1173 "Number of columns to indent the second line of a `(def...)' form."
1174 :group 'lisp
1175 :type 'integer)
1176 (put 'lisp-body-indent 'safe-local-variable 'integerp)
1178 (defun lisp-indent-specform (count state indent-point normal-indent)
1179 (let ((containing-form-start (elt state 1))
1180 (i count)
1181 body-indent containing-form-column)
1182 ;; Move to the start of containing form, calculate indentation
1183 ;; to use for non-distinguished forms (> count), and move past the
1184 ;; function symbol. lisp-indent-function guarantees that there is at
1185 ;; least one word or symbol character following open paren of containing
1186 ;; form.
1187 (goto-char containing-form-start)
1188 (setq containing-form-column (current-column))
1189 (setq body-indent (+ lisp-body-indent containing-form-column))
1190 (forward-char 1)
1191 (forward-sexp 1)
1192 ;; Now find the start of the last form.
1193 (parse-partial-sexp (point) indent-point 1 t)
1194 (while (and (< (point) indent-point)
1195 (condition-case ()
1196 (progn
1197 (setq count (1- count))
1198 (forward-sexp 1)
1199 (parse-partial-sexp (point) indent-point 1 t))
1200 (error nil))))
1201 ;; Point is sitting on first character of last (or count) sexp.
1202 (if (> count 0)
1203 ;; A distinguished form. If it is the first or second form use double
1204 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
1205 ;; to 2 (the default), this just happens to work the same with if as
1206 ;; the older code, but it makes unwind-protect, condition-case,
1207 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
1208 ;; less hacked, behavior can be obtained by replacing below with
1209 ;; (list normal-indent containing-form-start).
1210 (if (<= (- i count) 1)
1211 (list (+ containing-form-column (* 2 lisp-body-indent))
1212 containing-form-start)
1213 (list normal-indent containing-form-start))
1214 ;; A non-distinguished form. Use body-indent if there are no
1215 ;; distinguished forms and this is the first undistinguished form,
1216 ;; or if this is the first undistinguished form and the preceding
1217 ;; distinguished form has indentation at least as great as body-indent.
1218 (if (or (and (= i 0) (= count 0))
1219 (and (= count 0) (<= body-indent normal-indent)))
1220 body-indent
1221 normal-indent))))
1223 (defun lisp-indent-defform (state indent-point)
1224 (goto-char (car (cdr state)))
1225 (forward-line 1)
1226 (if (> (point) (car (cdr (cdr state))))
1227 (progn
1228 (goto-char (car (cdr state)))
1229 (+ lisp-body-indent (current-column)))))
1232 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1233 ;; like defun if the first form is placed on the next line, otherwise
1234 ;; it is indented like any other form (i.e. forms line up under first).
1236 (put 'lambda 'lisp-indent-function 'defun)
1237 (put 'autoload 'lisp-indent-function 'defun)
1238 (put 'progn 'lisp-indent-function 0)
1239 (put 'prog1 'lisp-indent-function 1)
1240 (put 'prog2 'lisp-indent-function 2)
1241 (put 'save-excursion 'lisp-indent-function 0)
1242 (put 'save-restriction 'lisp-indent-function 0)
1243 (put 'save-match-data 'lisp-indent-function 0)
1244 (put 'save-current-buffer 'lisp-indent-function 0)
1245 (put 'let 'lisp-indent-function 1)
1246 (put 'let* 'lisp-indent-function 1)
1247 (put 'while 'lisp-indent-function 1)
1248 (put 'if 'lisp-indent-function 2)
1249 (put 'catch 'lisp-indent-function 1)
1250 (put 'condition-case 'lisp-indent-function 2)
1251 (put 'unwind-protect 'lisp-indent-function 1)
1252 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1254 (defun indent-sexp (&optional endpos)
1255 "Indent each line of the list starting just after point.
1256 If optional arg ENDPOS is given, indent each line, stopping when
1257 ENDPOS is encountered."
1258 (interactive)
1259 (let ((indent-stack (list nil))
1260 (next-depth 0)
1261 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1262 ;; so that calculate-lisp-indent will find the beginning of
1263 ;; the defun we are in.
1264 ;; If ENDPOS is nil, it is safe not to scan before point
1265 ;; since every line we indent is more deeply nested than point is.
1266 (starting-point (if endpos nil (point)))
1267 (last-point (point))
1268 last-depth bol outer-loop-done inner-loop-done state this-indent)
1269 (or endpos
1270 ;; Get error now if we don't have a complete sexp after point.
1271 (save-excursion (forward-sexp 1)))
1272 (save-excursion
1273 (setq outer-loop-done nil)
1274 (while (if endpos (< (point) endpos)
1275 (not outer-loop-done))
1276 (setq last-depth next-depth
1277 inner-loop-done nil)
1278 ;; Parse this line so we can learn the state
1279 ;; to indent the next line.
1280 ;; This inner loop goes through only once
1281 ;; unless a line ends inside a string.
1282 (while (and (not inner-loop-done)
1283 (not (setq outer-loop-done (eobp))))
1284 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1285 nil nil state))
1286 (setq next-depth (car state))
1287 ;; If the line contains a comment other than the sort
1288 ;; that is indented like code,
1289 ;; indent it now with indent-for-comment.
1290 ;; Comments indented like code are right already.
1291 ;; In any case clear the in-comment flag in the state
1292 ;; because parse-partial-sexp never sees the newlines.
1293 (if (car (nthcdr 4 state))
1294 (progn (indent-for-comment)
1295 (end-of-line)
1296 (setcar (nthcdr 4 state) nil)))
1297 ;; If this line ends inside a string,
1298 ;; go straight to next line, remaining within the inner loop,
1299 ;; and turn off the \-flag.
1300 (if (car (nthcdr 3 state))
1301 (progn
1302 (forward-line 1)
1303 (setcar (nthcdr 5 state) nil))
1304 (setq inner-loop-done t)))
1305 (and endpos
1306 (<= next-depth 0)
1307 (progn
1308 (setq indent-stack (nconc indent-stack
1309 (make-list (- next-depth) nil))
1310 last-depth (- last-depth next-depth)
1311 next-depth 0)))
1312 (forward-line 1)
1313 ;; Decide whether to exit.
1314 (if endpos
1315 ;; If we have already reached the specified end,
1316 ;; give up and do not reindent this line.
1317 (if (<= endpos (point))
1318 (setq outer-loop-done t))
1319 ;; If no specified end, we are done if we have finished one sexp.
1320 (if (<= next-depth 0)
1321 (setq outer-loop-done t)))
1322 (unless outer-loop-done
1323 (while (> last-depth next-depth)
1324 (setq indent-stack (cdr indent-stack)
1325 last-depth (1- last-depth)))
1326 (while (< last-depth next-depth)
1327 (setq indent-stack (cons nil indent-stack)
1328 last-depth (1+ last-depth)))
1329 ;; Now indent the next line according
1330 ;; to what we learned from parsing the previous one.
1331 (setq bol (point))
1332 (skip-chars-forward " \t")
1333 ;; But not if the line is blank, or just a comment
1334 ;; (except for double-semi comments; indent them as usual).
1335 (if (or (eobp) (looking-at "\\s<\\|\n"))
1337 (if (and (car indent-stack)
1338 (>= (car indent-stack) 0))
1339 (setq this-indent (car indent-stack))
1340 (let ((val (calculate-lisp-indent
1341 (if (car indent-stack) (- (car indent-stack))
1342 starting-point))))
1343 (if (null val)
1344 (setq this-indent val)
1345 (if (integerp val)
1346 (setcar indent-stack
1347 (setq this-indent val))
1348 (setcar indent-stack (- (car (cdr val))))
1349 (setq this-indent (car val))))))
1350 (if (and this-indent (/= (current-column) this-indent))
1351 (progn (delete-region bol (point))
1352 (indent-to this-indent)))))
1353 (or outer-loop-done
1354 (setq outer-loop-done (= (point) last-point))
1355 (setq last-point (point)))))))
1357 (defun indent-pp-sexp (&optional arg)
1358 "Indent each line of the list starting just after point, or prettyprint it.
1359 A prefix argument specifies pretty-printing."
1360 (interactive "P")
1361 (if arg
1362 (save-excursion
1363 (save-restriction
1364 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1365 (pp-buffer)
1366 (goto-char (point-max))
1367 (if (eq (char-before) ?\n)
1368 (delete-char -1)))))
1369 (indent-sexp))
1371 ;;;; Lisp paragraph filling commands.
1373 (defcustom emacs-lisp-docstring-fill-column 65
1374 "Value of `fill-column' to use when filling a docstring.
1375 Any non-integer value means do not use a different value of
1376 `fill-column' when filling docstrings."
1377 :type '(choice (integer)
1378 (const :tag "Use the current `fill-column'" t))
1379 :group 'lisp)
1381 (defun lisp-fill-paragraph (&optional justify)
1382 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1383 If any of the current line is a comment, fill the comment or the
1384 paragraph of it that point is in, preserving the comment's indentation
1385 and initial semicolons."
1386 (interactive "P")
1387 (or (fill-comment-paragraph justify)
1388 ;; Since fill-comment-paragraph returned nil, that means we're not in
1389 ;; a comment: Point is on a program line; we are interested
1390 ;; particularly in docstring lines.
1392 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1393 ;; are buffer-local, but we avoid changing them so that they can be set
1394 ;; to make `forward-paragraph' and friends do something the user wants.
1396 ;; `paragraph-start': The `(' in the character alternative and the
1397 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1398 ;; sexps and backquoted sexps that follow a docstring from being filled
1399 ;; with the docstring. This setting has the consequence of inhibiting
1400 ;; filling many program lines that are not docstrings, which is sensible,
1401 ;; because the user probably asked to fill program lines by accident, or
1402 ;; expecting indentation (perhaps we should try to do indenting in that
1403 ;; case). The `;' and `:' stop the paragraph being filled at following
1404 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1405 ;; escaped to keep font-locking, filling, & paren matching in the source
1406 ;; file happy.
1408 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1409 ;; a docstring and identifies it as a paragraph separator, so that it
1410 ;; won't be filled. (Since the first line of documentation stands alone
1411 ;; in some contexts, filling should not alter the contents the author has
1412 ;; chosen.) Only the first line of a docstring begins with whitespace
1413 ;; and a quotation mark and ends with a period or (rarely) a comma.
1415 ;; The `fill-column' is temporarily bound to
1416 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1417 (let ((paragraph-start (concat paragraph-start
1418 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1419 (paragraph-separate
1420 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1421 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1422 (derived-mode-p 'emacs-lisp-mode))
1423 emacs-lisp-docstring-fill-column
1424 fill-column)))
1425 (fill-paragraph justify))
1426 ;; Never return nil.
1429 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1430 "Indent all lines of code, starting in the region, sideways by ARG columns.
1431 Does not affect lines starting inside comments or strings, assuming that
1432 the start of the region is not inside them.
1434 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1435 The last is a regexp which, if matched at the beginning of a line,
1436 means don't indent that line."
1437 (interactive "r\np")
1438 (let (state)
1439 (save-excursion
1440 (goto-char end)
1441 (setq end (point-marker))
1442 (goto-char start)
1443 (or (bolp)
1444 (setq state (parse-partial-sexp (point)
1445 (progn
1446 (forward-line 1) (point))
1447 nil nil state)))
1448 (while (< (point) end)
1449 (or (car (nthcdr 3 state))
1450 (and nochange-regexp
1451 (looking-at nochange-regexp))
1452 ;; If line does not start in string, indent it
1453 (let ((indent (current-indentation)))
1454 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1455 (or (eolp)
1456 (indent-to (max 0 (+ indent arg)) 0))))
1457 (setq state (parse-partial-sexp (point)
1458 (progn
1459 (forward-line 1) (point))
1460 nil nil state))))))
1462 (provide 'lisp-mode)
1464 ;;; lisp-mode.el ends here