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