Don't have help functions call x-display-pixel-width/-height on ttys
[emacs.git] / lisp / emacs-lisp / lisp-mode.el
blob9ce0dfd49e84b1f0665867dc98846fec8c839898
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1986, 1999-2015 Free Software Foundation, Inc.
5 ;; Maintainer: emacs-devel@gnu.org
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 (eval-when-compile (require 'cl-lib))
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 (define-abbrev-table 'lisp-mode-abbrev-table ()
39 "Abbrev table for Lisp mode.")
41 (defvar lisp--mode-syntax-table
42 (let ((table (make-syntax-table))
43 (i 0))
44 (while (< i ?0)
45 (modify-syntax-entry i "_ " table)
46 (setq i (1+ i)))
47 (setq i (1+ ?9))
48 (while (< i ?A)
49 (modify-syntax-entry i "_ " table)
50 (setq i (1+ i)))
51 (setq i (1+ ?Z))
52 (while (< i ?a)
53 (modify-syntax-entry i "_ " table)
54 (setq i (1+ i)))
55 (setq i (1+ ?z))
56 (while (< i 128)
57 (modify-syntax-entry i "_ " table)
58 (setq i (1+ i)))
59 (modify-syntax-entry ?\s " " table)
60 ;; Non-break space acts as whitespace.
61 (modify-syntax-entry ?\x8a0 " " table)
62 (modify-syntax-entry ?\t " " table)
63 (modify-syntax-entry ?\f " " table)
64 (modify-syntax-entry ?\n "> " table)
65 ;; This is probably obsolete since nowadays such features use overlays.
66 ;; ;; Give CR the same syntax as newline, for selective-display.
67 ;; (modify-syntax-entry ?\^m "> " table)
68 (modify-syntax-entry ?\; "< " table)
69 (modify-syntax-entry ?` "' " table)
70 (modify-syntax-entry ?' "' " table)
71 (modify-syntax-entry ?, "' " table)
72 (modify-syntax-entry ?@ "_ p" table)
73 ;; Used to be singlequote; changed for flonums.
74 (modify-syntax-entry ?. "_ " table)
75 (modify-syntax-entry ?# "' " table)
76 (modify-syntax-entry ?\" "\" " table)
77 (modify-syntax-entry ?\\ "\\ " table)
78 (modify-syntax-entry ?\( "() " table)
79 (modify-syntax-entry ?\) ")( " table)
80 table)
81 "Parent syntax table used in Lisp modes.")
83 (defvar lisp-mode-syntax-table
84 (let ((table (make-syntax-table lisp--mode-syntax-table)))
85 (modify-syntax-entry ?\[ "_ " table)
86 (modify-syntax-entry ?\] "_ " table)
87 (modify-syntax-entry ?# "' 14" table)
88 (modify-syntax-entry ?| "\" 23bn" table)
89 table)
90 "Syntax table used in `lisp-mode'.")
92 (eval-and-compile
93 (defconst lisp-mode-symbol-regexp "\\(?:\\sw\\|\\s_\\|\\\\.\\)+"))
95 (defvar lisp-imenu-generic-expression
96 (list
97 (list nil
98 (purecopy (concat "^\\s-*("
99 (eval-when-compile
100 (regexp-opt
101 '("defun" "defmacro"
102 ;; Elisp.
103 "defun*" "defsubst" "define-inline"
104 "define-advice" "defadvice" "define-skeleton"
105 "define-compilation-mode" "define-minor-mode"
106 "define-global-minor-mode"
107 "define-globalized-minor-mode"
108 "define-derived-mode" "define-generic-mode"
109 "cl-defun" "cl-defsubst" "cl-defmacro"
110 "cl-define-compiler-macro"
111 ;; CL.
112 "define-compiler-macro" "define-modify-macro"
113 "defsetf" "define-setf-expander"
114 "define-method-combination"
115 ;; CLOS and EIEIO
116 "defgeneric" "defmethod")
118 "\\s-+\\(" lisp-mode-symbol-regexp "\\)"))
120 (list (purecopy "Variables")
121 (purecopy (concat "^\\s-*("
122 (eval-when-compile
123 (regexp-opt
124 '(;; Elisp
125 "defconst" "defcustom"
126 ;; CL
127 "defconstant"
128 "defparameter" "define-symbol-macro")
130 "\\s-+\\(" lisp-mode-symbol-regexp "\\)"))
132 ;; For `defvar', we ignore (defvar FOO) constructs.
133 (list (purecopy "Variables")
134 (purecopy (concat "^\\s-*(defvar\\s-+\\(" lisp-mode-symbol-regexp "\\)"
135 "[[:space:]\n]+[^)]"))
137 (list (purecopy "Types")
138 (purecopy (concat "^\\s-*("
139 (eval-when-compile
140 (regexp-opt
141 '(;; Elisp
142 "defgroup" "deftheme"
143 "define-widget" "define-error"
144 "defface" "cl-deftype" "cl-defstruct"
145 ;; CL
146 "deftype" "defstruct"
147 "define-condition" "defpackage"
148 ;; CLOS and EIEIO
149 "defclass")
151 "\\s-+'?\\(" lisp-mode-symbol-regexp "\\)"))
154 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.")
156 ;; This was originally in autoload.el and is still used there.
157 (put 'autoload 'doc-string-elt 3)
158 (put 'defmethod 'doc-string-elt 3)
159 (put 'defvar 'doc-string-elt 3)
160 (put 'defconst 'doc-string-elt 3)
161 (put 'defalias 'doc-string-elt 3)
162 (put 'defvaralias 'doc-string-elt 3)
163 (put 'define-category 'doc-string-elt 2)
165 (defvar lisp-doc-string-elt-property 'doc-string-elt
166 "The symbol property that holds the docstring position info.")
169 ;;;; Font-lock support.
171 (defun lisp--match-hidden-arg (limit)
172 (let ((res nil))
173 (while
174 (let ((ppss (parse-partial-sexp (line-beginning-position)
175 (line-end-position)
176 -1)))
177 (skip-syntax-forward " )")
178 (if (or (>= (car ppss) 0)
179 (looking-at ";\\|$"))
180 (progn
181 (forward-line 1)
182 (< (point) limit))
183 (looking-at ".*") ;Set the match-data.
184 (forward-line 1)
185 (setq res (point))
186 nil)))
187 res))
189 (defun lisp--el-non-funcall-position-p (pos)
190 "Heuristically determine whether POS is an evaluated position."
191 (save-match-data
192 (save-excursion
193 (ignore-errors
194 (goto-char pos)
195 (or (eql (char-before) ?\')
196 (let* ((ppss (syntax-ppss))
197 (paren-posns (nth 9 ppss))
198 (parent
199 (when paren-posns
200 (goto-char (car (last paren-posns))) ;(up-list -1)
201 (cond
202 ((ignore-errors
203 (and (eql (char-after) ?\()
204 (when (cdr paren-posns)
205 (goto-char (car (last paren-posns 2)))
206 (looking-at "(\\_<let\\*?\\_>"))))
207 (goto-char (match-end 0))
208 'let)
209 ((looking-at
210 (rx "("
211 (group-n 1 (+ (or (syntax w) (syntax _))))
212 symbol-end))
213 (prog1 (intern-soft (match-string-no-properties 1))
214 (goto-char (match-end 1))))))))
215 (or (eq parent 'declare)
216 (and (eq parent 'let)
217 (progn
218 (forward-sexp 1)
219 (< pos (point))))
220 (and (eq parent 'condition-case)
221 (progn
222 (forward-sexp 2)
223 (< (point) pos))))))))))
225 (defun lisp--el-match-keyword (limit)
226 ;; FIXME: Move to elisp-mode.el.
227 (catch 'found
228 (while (re-search-forward
229 (eval-when-compile
230 (concat "(\\(" lisp-mode-symbol-regexp "\\)\\_>"))
231 limit t)
232 (let ((sym (intern-soft (match-string 1))))
233 (when (or (special-form-p sym)
234 (and (macrop sym)
235 (not (get sym 'no-font-lock-keyword))
236 (not (lisp--el-non-funcall-position-p
237 (match-beginning 0)))))
238 (throw 'found t))))))
240 (defmacro let-when-compile (bindings &rest body)
241 "Like `let*', but allow for compile time optimization.
242 Use BINDINGS as in regular `let*', but in BODY each usage should
243 be wrapped in `eval-when-compile'.
244 This will generate compile-time constants from BINDINGS."
245 (declare (indent 1) (debug let))
246 (letrec ((loop
247 (lambda (bindings)
248 (if (null bindings)
249 (macroexpand-all (macroexp-progn body)
250 macroexpand-all-environment)
251 (let ((binding (pop bindings)))
252 (cl-progv (list (car binding))
253 (list (eval (nth 1 binding) t))
254 (funcall loop bindings)))))))
255 (funcall loop bindings)))
257 (let-when-compile
258 ((lisp-fdefs '("defmacro" "defun"))
259 (lisp-vdefs '("defvar"))
260 (lisp-kw '("cond" "if" "while" "let" "let*" "progn" "prog1"
261 "prog2" "lambda" "unwind-protect" "condition-case"
262 "when" "unless" "with-output-to-string"
263 "ignore-errors" "dotimes" "dolist" "declare"))
264 (lisp-errs '("warn" "error" "signal"))
265 ;; Elisp constructs. Now they are update dynamically
266 ;; from obarray but they are also used for setting up
267 ;; the keywords for Common Lisp.
268 (el-fdefs '("defsubst" "cl-defsubst" "define-inline"
269 "define-advice" "defadvice" "defalias"
270 "define-derived-mode" "define-minor-mode"
271 "define-generic-mode" "define-global-minor-mode"
272 "define-globalized-minor-mode" "define-skeleton"
273 "define-widget"))
274 (el-vdefs '("defconst" "defcustom" "defvaralias" "defvar-local"
275 "defface"))
276 (el-tdefs '("defgroup" "deftheme"))
277 (el-errs '("user-error"))
278 ;; Common-Lisp constructs supported by EIEIO. FIXME: namespace.
279 (eieio-fdefs '("defgeneric" "defmethod"))
280 (eieio-tdefs '("defclass"))
281 ;; Common-Lisp constructs supported by cl-lib.
282 (cl-lib-fdefs '("defmacro" "defsubst" "defun" "defmethod" "defgeneric"))
283 (cl-lib-tdefs '("defstruct" "deftype"))
284 (cl-lib-errs '("assert" "check-type"))
285 ;; Common-Lisp constructs not supported by cl-lib.
286 (cl-fdefs '("defsetf" "define-method-combination"
287 "define-condition" "define-setf-expander"
288 ;; "define-function"??
289 "define-compiler-macro" "define-modify-macro"))
290 (cl-vdefs '("define-symbol-macro" "defconstant" "defparameter"))
291 (cl-tdefs '("defpackage" "defstruct" "deftype"))
292 (cl-kw '("block" "break" "case" "ccase" "compiler-let" "ctypecase"
293 "declaim" "destructuring-bind" "do" "do*"
294 "ecase" "etypecase" "eval-when" "flet" "flet*"
295 "go" "handler-case" "handler-bind" "in-package" ;; "inline"
296 "labels" "letf" "locally" "loop"
297 "macrolet" "multiple-value-bind" "multiple-value-prog1"
298 "proclaim" "prog" "prog*" "progv"
299 "restart-case" "restart-bind" "return" "return-from"
300 "symbol-macrolet" "tagbody" "the" "typecase"
301 "with-accessors" "with-compilation-unit"
302 "with-condition-restarts" "with-hash-table-iterator"
303 "with-input-from-string" "with-open-file"
304 "with-open-stream" "with-package-iterator"
305 "with-simple-restart" "with-slots" "with-standard-io-syntax"))
306 (cl-errs '("abort" "cerror")))
307 (let ((vdefs (eval-when-compile
308 (append lisp-vdefs el-vdefs cl-vdefs)))
309 (tdefs (eval-when-compile
310 (append el-tdefs eieio-tdefs cl-tdefs cl-lib-tdefs
311 (mapcar (lambda (s) (concat "cl-" s)) cl-lib-tdefs))))
312 ;; Elisp and Common Lisp definers.
313 (el-defs-re (eval-when-compile
314 (regexp-opt (append lisp-fdefs lisp-vdefs
315 el-fdefs el-vdefs el-tdefs
316 (mapcar (lambda (s) (concat "cl-" s))
317 (append cl-lib-fdefs cl-lib-tdefs))
318 eieio-fdefs eieio-tdefs)
319 t)))
320 (cl-defs-re (eval-when-compile
321 (regexp-opt (append lisp-fdefs lisp-vdefs
322 cl-lib-fdefs cl-lib-tdefs
323 eieio-fdefs eieio-tdefs
324 cl-fdefs cl-vdefs cl-tdefs)
325 t)))
326 ;; Common Lisp keywords (Elisp keywords are handled dynamically).
327 (cl-kws-re (eval-when-compile
328 (regexp-opt (append lisp-kw cl-kw) t)))
329 ;; Elisp and Common Lisp "errors".
330 (el-errs-re (eval-when-compile
331 (regexp-opt (append (mapcar (lambda (s) (concat "cl-" s))
332 cl-lib-errs)
333 lisp-errs el-errs)
334 t)))
335 (cl-errs-re (eval-when-compile
336 (regexp-opt (append lisp-errs cl-lib-errs cl-errs) t))))
337 (dolist (v vdefs)
338 (put (intern v) 'lisp-define-type 'var))
339 (dolist (v tdefs)
340 (put (intern v) 'lisp-define-type 'type))
342 (define-obsolete-variable-alias 'lisp-font-lock-keywords-1
343 'lisp-el-font-lock-keywords-1 "24.4")
344 (defconst lisp-el-font-lock-keywords-1
345 `( ;; Definitions.
346 (,(concat "(" el-defs-re "\\_>"
347 ;; Any whitespace and defined object.
348 "[ \t']*"
349 "\\(([ \t']*\\)?" ;; An opening paren.
350 "\\(\\(setf\\)[ \t]+" lisp-mode-symbol-regexp
351 "\\|" lisp-mode-symbol-regexp "\\)?")
352 (1 font-lock-keyword-face)
353 (3 (let ((type (get (intern-soft (match-string 1)) 'lisp-define-type)))
354 (cond ((eq type 'var) font-lock-variable-name-face)
355 ((eq type 'type) font-lock-type-face)
356 ;; If match-string 2 is non-nil, we encountered a
357 ;; form like (defalias (intern (concat s "-p"))),
358 ;; unless match-string 4 is also there. Then its a
359 ;; defmethod with (setf foo) as name.
360 ((or (not (match-string 2)) ;; Normal defun.
361 (and (match-string 2) ;; Setf method.
362 (match-string 4)))
363 font-lock-function-name-face)))
364 nil t))
365 ;; Emacs Lisp autoload cookies. Supports the slightly different
366 ;; forms used by mh-e, calendar, etc.
367 ("^;;;###\\([-a-z]*autoload\\)" 1 font-lock-warning-face prepend))
368 "Subdued level highlighting for Emacs Lisp mode.")
370 (defconst lisp-cl-font-lock-keywords-1
371 `( ;; Definitions.
372 (,(concat "(" cl-defs-re "\\_>"
373 ;; Any whitespace and defined object.
374 "[ \t']*"
375 "\\(([ \t']*\\)?" ;; An opening paren.
376 "\\(\\(setf\\)[ \t]+" lisp-mode-symbol-regexp
377 "\\|" lisp-mode-symbol-regexp "\\)?")
378 (1 font-lock-keyword-face)
379 (3 (let ((type (get (intern-soft (match-string 1)) 'lisp-define-type)))
380 (cond ((eq type 'var) font-lock-variable-name-face)
381 ((eq type 'type) font-lock-type-face)
382 ((or (not (match-string 2)) ;; Normal defun.
383 (and (match-string 2) ;; Setf function.
384 (match-string 4))) font-lock-function-name-face)))
385 nil t)))
386 "Subdued level highlighting for Lisp modes.")
388 (define-obsolete-variable-alias 'lisp-font-lock-keywords-2
389 'lisp-el-font-lock-keywords-2 "24.4")
390 (defconst lisp-el-font-lock-keywords-2
391 (append
392 lisp-el-font-lock-keywords-1
393 `( ;; Regexp negated char group.
394 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)
395 ;; Control structures. Common Lisp forms.
396 (lisp--el-match-keyword . 1)
397 ;; Exit/Feature symbols as constants.
398 (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\_>"
399 "[ \t']*\\(" lisp-mode-symbol-regexp "\\)?")
400 (1 font-lock-keyword-face)
401 (2 font-lock-constant-face nil t))
402 ;; Erroneous structures.
403 (,(concat "(" el-errs-re "\\_>")
404 (1 font-lock-warning-face))
405 ;; Words inside \\[] tend to be for `substitute-command-keys'.
406 (,(concat "\\\\\\\\\\[\\(" lisp-mode-symbol-regexp "\\)\\]")
407 (1 font-lock-constant-face prepend))
408 ;; Words inside ‘’ and '' and `' tend to be symbol names.
409 (,(concat "['`‘]\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)"
410 lisp-mode-symbol-regexp "\\)['’]")
411 (1 font-lock-constant-face prepend))
412 ;; Constant values.
413 (,(concat "\\_<:" lisp-mode-symbol-regexp "\\_>")
414 (0 font-lock-builtin-face))
415 ;; ELisp and CLisp `&' keywords as types.
416 (,(concat "\\_<\\&" lisp-mode-symbol-regexp "\\_>")
417 . font-lock-type-face)
418 ;; ELisp regexp grouping constructs
419 (,(lambda (bound)
420 (catch 'found
421 ;; The following loop is needed to continue searching after matches
422 ;; that do not occur in strings. The associated regexp matches one
423 ;; of `\\\\' `\\(' `\\(?:' `\\|' `\\)'. `\\\\' has been included to
424 ;; avoid highlighting, for example, `\\(' in `\\\\('.
425 (while (re-search-forward "\\(\\\\\\\\\\)\\(?:\\(\\\\\\\\\\)\\|\\((\\(?:\\?[0-9]*:\\)?\\|[|)]\\)\\)" bound t)
426 (unless (match-beginning 2)
427 (let ((face (get-text-property (1- (point)) 'face)))
428 (when (or (and (listp face)
429 (memq 'font-lock-string-face face))
430 (eq 'font-lock-string-face face))
431 (throw 'found t)))))))
432 (1 'font-lock-regexp-grouping-backslash prepend)
433 (3 'font-lock-regexp-grouping-construct prepend))
434 ;; This is too general -- rms.
435 ;; A user complained that he has functions whose names start with `do'
436 ;; and that they get the wrong color.
437 ;; ;; CL `with-' and `do-' constructs
438 ;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
439 (lisp--match-hidden-arg
440 (0 '(face font-lock-warning-face
441 help-echo "Hidden behind deeper element; move to another line?")))
443 "Gaudy level highlighting for Emacs Lisp mode.")
445 (defconst lisp-cl-font-lock-keywords-2
446 (append
447 lisp-cl-font-lock-keywords-1
448 `( ;; Regexp negated char group.
449 ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)
450 ;; Control structures. Common Lisp forms.
451 (,(concat "(" cl-kws-re "\\_>") . 1)
452 ;; Exit/Feature symbols as constants.
453 (,(concat "(\\(catch\\|throw\\|provide\\|require\\)\\_>"
454 "[ \t']*\\(" lisp-mode-symbol-regexp "\\)?")
455 (1 font-lock-keyword-face)
456 (2 font-lock-constant-face nil t))
457 ;; Erroneous structures.
458 (,(concat "(" cl-errs-re "\\_>")
459 (1 font-lock-warning-face))
460 ;; Words inside ‘’ and '' and `' tend to be symbol names.
461 (,(concat "['`‘]\\(\\(?:\\sw\\|\\s_\\|\\\\.\\)"
462 lisp-mode-symbol-regexp "\\)['’]")
463 (1 font-lock-constant-face prepend))
464 ;; Constant values.
465 (,(concat "\\_<:" lisp-mode-symbol-regexp "\\_>")
466 (0 font-lock-builtin-face))
467 ;; ELisp and CLisp `&' keywords as types.
468 (,(concat "\\_<\\&" lisp-mode-symbol-regexp "\\_>")
469 . font-lock-type-face)
470 ;; This is too general -- rms.
471 ;; A user complained that he has functions whose names start with `do'
472 ;; and that they get the wrong color.
473 ;; ;; CL `with-' and `do-' constructs
474 ;;("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
475 (lisp--match-hidden-arg
476 (0 '(face font-lock-warning-face
477 help-echo "Hidden behind deeper element; move to another line?")))
479 "Gaudy level highlighting for Lisp modes.")))
481 (define-obsolete-variable-alias 'lisp-font-lock-keywords
482 'lisp-el-font-lock-keywords "24.4")
483 (defvar lisp-el-font-lock-keywords lisp-el-font-lock-keywords-1
484 "Default expressions to highlight in Emacs Lisp mode.")
485 (defvar lisp-cl-font-lock-keywords lisp-cl-font-lock-keywords-1
486 "Default expressions to highlight in Lisp modes.")
488 (defun lisp-string-in-doc-position-p (listbeg startpos)
489 (let* ((firstsym (and listbeg
490 (save-excursion
491 (goto-char listbeg)
492 (and (looking-at
493 (eval-when-compile
494 (concat "([ \t\n]*\\("
495 lisp-mode-symbol-regexp "\\)")))
496 (match-string 1)))))
497 (docelt (and firstsym
498 (function-get (intern-soft firstsym)
499 lisp-doc-string-elt-property))))
500 (and docelt
501 ;; It's a string in a form that can have a docstring.
502 ;; Check whether it's in docstring position.
503 (save-excursion
504 (when (functionp docelt)
505 (goto-char (match-end 1))
506 (setq docelt (funcall docelt)))
507 (goto-char listbeg)
508 (forward-char 1)
509 (condition-case nil
510 (while (and (> docelt 0) (< (point) startpos)
511 (progn (forward-sexp 1) t))
512 (setq docelt (1- docelt)))
513 (error nil))
514 (and (zerop docelt) (<= (point) startpos)
515 (progn (forward-comment (point-max)) t)
516 (= (point) startpos))))))
518 (defun lisp-string-after-doc-keyword-p (listbeg startpos)
519 (and listbeg ; We are inside a Lisp form.
520 (save-excursion
521 (goto-char startpos)
522 (ignore-errors
523 (progn (backward-sexp 1)
524 (looking-at ":documentation\\_>"))))))
526 (defun lisp-font-lock-syntactic-face-function (state)
527 (if (nth 3 state)
528 ;; This might be a (doc)string or a |...| symbol.
529 (let ((startpos (nth 8 state)))
530 (if (eq (char-after startpos) ?|)
531 ;; This is not a string, but a |...| symbol.
533 (let ((listbeg (nth 1 state)))
534 (if (or (lisp-string-in-doc-position-p listbeg startpos)
535 (lisp-string-after-doc-keyword-p listbeg startpos))
536 font-lock-doc-face
537 font-lock-string-face))))
538 font-lock-comment-face))
540 (defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive
541 elisp)
542 "Common initialization routine for lisp modes.
543 The LISP-SYNTAX argument is used by code in inf-lisp.el and is
544 \(uselessly) passed from pp.el, chistory.el, gnus-kill.el and
545 score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for
546 font-lock keywords will not be case sensitive."
547 (when lisp-syntax
548 (set-syntax-table lisp-mode-syntax-table))
549 (setq-local paragraph-ignore-fill-prefix t)
550 (setq-local fill-paragraph-function 'lisp-fill-paragraph)
551 ;; Adaptive fill mode gets the fill wrong for a one-line paragraph made of
552 ;; a single docstring. Let's fix it here.
553 (setq-local adaptive-fill-function
554 (lambda () (if (looking-at "\\s-+\"[^\n\"]+\"\\s-*$") "")))
555 ;; Adaptive fill mode gets in the way of auto-fill,
556 ;; and should make no difference for explicit fill
557 ;; because lisp-fill-paragraph should do the job.
558 ;; I believe that newcomment's auto-fill code properly deals with it -stef
559 ;;(set (make-local-variable 'adaptive-fill-mode) nil)
560 (setq-local indent-line-function 'lisp-indent-line)
561 (setq-local outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(")
562 (setq-local outline-level 'lisp-outline-level)
563 (setq-local add-log-current-defun-function #'lisp-current-defun-name)
564 (setq-local comment-start ";")
565 (setq-local comment-start-skip ";+ *")
566 (setq-local comment-add 1) ;default to `;;' in comment-region
567 (setq-local comment-column 40)
568 (setq-local comment-use-syntax t)
569 (setq-local imenu-generic-expression lisp-imenu-generic-expression)
570 (setq-local multibyte-syntax-as-symbol t)
571 ;; (setq-local syntax-begin-function 'beginning-of-defun) ;;Bug#16247.
572 (setq font-lock-defaults
573 `(,(if elisp '(lisp-el-font-lock-keywords
574 lisp-el-font-lock-keywords-1
575 lisp-el-font-lock-keywords-2)
576 '(lisp-cl-font-lock-keywords
577 lisp-cl-font-lock-keywords-1
578 lisp-cl-font-lock-keywords-2))
579 nil ,keywords-case-insensitive nil nil
580 (font-lock-mark-block-function . mark-defun)
581 (font-lock-extra-managed-props help-echo)
582 (font-lock-syntactic-face-function
583 . lisp-font-lock-syntactic-face-function)))
584 (setq-local prettify-symbols-alist lisp--prettify-symbols-alist)
585 (setq-local electric-pair-skip-whitespace 'chomp)
586 (setq-local electric-pair-open-newline-between-pairs nil))
588 (defun lisp-outline-level ()
589 "Lisp mode `outline-level' function."
590 (let ((len (- (match-end 0) (match-beginning 0))))
591 (if (looking-at "(\\|;;;###autoload")
592 1000
593 len)))
595 (defun lisp-current-defun-name ()
596 "Return the name of the defun at point, or nil."
597 (save-excursion
598 (let ((location (point)))
599 ;; If we are now precisely at the beginning of a defun, make sure
600 ;; beginning-of-defun finds that one rather than the previous one.
601 (or (eobp) (forward-char 1))
602 (beginning-of-defun)
603 ;; Make sure we are really inside the defun found, not after it.
604 (when (and (looking-at "\\s(")
605 (progn (end-of-defun)
606 (< location (point)))
607 (progn (forward-sexp -1)
608 (>= location (point))))
609 (if (looking-at "\\s(")
610 (forward-char 1))
611 ;; Skip the defining construct name, typically "defun" or
612 ;; "defvar".
613 (forward-sexp 1)
614 ;; The second element is usually a symbol being defined. If it
615 ;; is not, use the first symbol in it.
616 (skip-chars-forward " \t\n'(")
617 (buffer-substring-no-properties (point)
618 (progn (forward-sexp 1)
619 (point)))))))
621 (defvar lisp-mode-shared-map
622 (let ((map (make-sparse-keymap)))
623 (set-keymap-parent map prog-mode-map)
624 (define-key map "\e\C-q" 'indent-sexp)
625 (define-key map "\177" 'backward-delete-char-untabify)
626 ;; This gets in the way when viewing a Lisp file in view-mode. As
627 ;; long as [backspace] is mapped into DEL via the
628 ;; function-key-map, this should remain disabled!!
629 ;;;(define-key map [backspace] 'backward-delete-char-untabify)
630 map)
631 "Keymap for commands shared by all sorts of Lisp modes.")
633 (defcustom lisp-mode-hook nil
634 "Hook run when entering Lisp mode."
635 :options '(imenu-add-menubar-index)
636 :type 'hook
637 :group 'lisp)
639 (defcustom lisp-interaction-mode-hook nil
640 "Hook run when entering Lisp Interaction mode."
641 :options '(eldoc-mode)
642 :type 'hook
643 :group 'lisp)
645 (defconst lisp--prettify-symbols-alist
646 '(("lambda" . ?λ)))
648 ;;; Generic Lisp mode.
650 (defvar lisp-mode-map
651 (let ((map (make-sparse-keymap))
652 (menu-map (make-sparse-keymap "Lisp")))
653 (set-keymap-parent map lisp-mode-shared-map)
654 (define-key map "\e\C-x" 'lisp-eval-defun)
655 (define-key map "\C-c\C-z" 'run-lisp)
656 (bindings--define-key map [menu-bar lisp] (cons "Lisp" menu-map))
657 (bindings--define-key menu-map [run-lisp]
658 '(menu-item "Run inferior Lisp" run-lisp
659 :help "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))
660 (bindings--define-key menu-map [ev-def]
661 '(menu-item "Eval defun" lisp-eval-defun
662 :help "Send the current defun to the Lisp process made by M-x run-lisp"))
663 (bindings--define-key menu-map [ind-sexp]
664 '(menu-item "Indent sexp" indent-sexp
665 :help "Indent each line of the list starting just after point"))
666 map)
667 "Keymap for ordinary Lisp mode.
668 All commands in `lisp-mode-shared-map' are inherited by this map.")
670 (define-derived-mode lisp-mode prog-mode "Lisp"
671 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
672 Commands:
673 Delete converts tabs to spaces as it moves back.
674 Blank lines separate paragraphs. Semicolons start comments.
676 \\{lisp-mode-map}
677 Note that `run-lisp' may be used either to start an inferior Lisp job
678 or to switch back to an existing one."
679 (lisp-mode-variables nil t)
680 (setq-local find-tag-default-function 'lisp-find-tag-default)
681 (setq-local comment-start-skip
682 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
683 (setq imenu-case-fold-search t))
685 (defun lisp-find-tag-default ()
686 (let ((default (find-tag-default)))
687 (when (stringp default)
688 (if (string-match ":+" default)
689 (substring default (match-end 0))
690 default))))
692 ;; Used in old LispM code.
693 (defalias 'common-lisp-mode 'lisp-mode)
695 ;; This will do unless inf-lisp.el is loaded.
696 (defun lisp-eval-defun (&optional _and-go)
697 "Send the current defun to the Lisp process made by \\[run-lisp]."
698 (interactive)
699 (error "Process lisp does not exist"))
701 ;; May still be used by some external Lisp-mode variant.
702 (define-obsolete-function-alias 'lisp-comment-indent
703 'comment-indent-default "22.1")
704 (define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1")
706 (defcustom lisp-indent-offset nil
707 "If non-nil, indent second line of expressions that many more columns."
708 :group 'lisp
709 :type '(choice (const nil) integer))
710 (put 'lisp-indent-offset 'safe-local-variable
711 (lambda (x) (or (null x) (integerp x))))
713 (defcustom lisp-indent-function 'lisp-indent-function
714 "A function to be called by `calculate-lisp-indent'.
715 It indents the arguments of a Lisp function call. This function
716 should accept two arguments: the indent-point, and the
717 `parse-partial-sexp' state at that position. One option for this
718 function is `common-lisp-indent-function'."
719 :type 'function
720 :group 'lisp)
722 (defun lisp-indent-line (&optional _whole-exp)
723 "Indent current line as Lisp code.
724 With argument, indent any additional lines of the same expression
725 rigidly along with this one."
726 (interactive "P")
727 (let ((indent (calculate-lisp-indent)) shift-amt
728 (pos (- (point-max) (point)))
729 (beg (progn (beginning-of-line) (point))))
730 (skip-chars-forward " \t")
731 (if (or (null indent) (looking-at "\\s<\\s<\\s<"))
732 ;; Don't alter indentation of a ;;; comment line
733 ;; or a line that starts in a string.
734 ;; FIXME: inconsistency: comment-indent moves ;;; to column 0.
735 (goto-char (- (point-max) pos))
736 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
737 ;; Single-semicolon comment lines should be indented
738 ;; as comment lines, not as code.
739 (progn (indent-for-comment) (forward-char -1))
740 (if (listp indent) (setq indent (car indent)))
741 (setq shift-amt (- indent (current-column)))
742 (if (zerop shift-amt)
744 (delete-region beg (point))
745 (indent-to indent)))
746 ;; If initial point was within line's indentation,
747 ;; position after the indentation. Else stay at same point in text.
748 (if (> (- (point-max) pos) (point))
749 (goto-char (- (point-max) pos))))))
751 (defvar calculate-lisp-indent-last-sexp)
753 (defun calculate-lisp-indent (&optional parse-start)
754 "Return appropriate indentation for current line as Lisp code.
755 In usual case returns an integer: the column to indent to.
756 If the value is nil, that means don't change the indentation
757 because the line starts inside a string.
759 The value can also be a list of the form (COLUMN CONTAINING-SEXP-START).
760 This means that following lines at the same level of indentation
761 should not necessarily be indented the same as this line.
762 Then COLUMN is the column to indent to, and CONTAINING-SEXP-START
763 is the buffer position of the start of the containing expression."
764 (save-excursion
765 (beginning-of-line)
766 (let ((indent-point (point))
767 state
768 ;; setting this to a number inhibits calling hook
769 (desired-indent nil)
770 (retry t)
771 calculate-lisp-indent-last-sexp containing-sexp)
772 (if parse-start
773 (goto-char parse-start)
774 (beginning-of-defun))
775 ;; Find outermost containing sexp
776 (while (< (point) indent-point)
777 (setq state (parse-partial-sexp (point) indent-point 0)))
778 ;; Find innermost containing sexp
779 (while (and retry
780 state
781 (> (elt state 0) 0))
782 (setq retry nil)
783 (setq calculate-lisp-indent-last-sexp (elt state 2))
784 (setq containing-sexp (elt state 1))
785 ;; Position following last unclosed open.
786 (goto-char (1+ containing-sexp))
787 ;; Is there a complete sexp since then?
788 (if (and calculate-lisp-indent-last-sexp
789 (> calculate-lisp-indent-last-sexp (point)))
790 ;; Yes, but is there a containing sexp after that?
791 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp
792 indent-point 0)))
793 (if (setq retry (car (cdr peek))) (setq state peek)))))
794 (if retry
796 ;; Innermost containing sexp found
797 (goto-char (1+ containing-sexp))
798 (if (not calculate-lisp-indent-last-sexp)
799 ;; indent-point immediately follows open paren.
800 ;; Don't call hook.
801 (setq desired-indent (current-column))
802 ;; Find the start of first element of containing sexp.
803 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
804 (cond ((looking-at "\\s(")
805 ;; First element of containing sexp is a list.
806 ;; Indent under that list.
808 ((> (save-excursion (forward-line 1) (point))
809 calculate-lisp-indent-last-sexp)
810 ;; This is the first line to start within the containing sexp.
811 ;; It's almost certainly a function call.
812 (if (= (point) calculate-lisp-indent-last-sexp)
813 ;; Containing sexp has nothing before this line
814 ;; except the first element. Indent under that element.
816 ;; Skip the first element, find start of second (the first
817 ;; argument of the function call) and indent under.
818 (progn (forward-sexp 1)
819 (parse-partial-sexp (point)
820 calculate-lisp-indent-last-sexp
821 0 t)))
822 (backward-prefix-chars))
824 ;; Indent beneath first sexp on same line as
825 ;; `calculate-lisp-indent-last-sexp'. Again, it's
826 ;; almost certainly a function call.
827 (goto-char calculate-lisp-indent-last-sexp)
828 (beginning-of-line)
829 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp
830 0 t)
831 (backward-prefix-chars)))))
832 ;; Point is at the point to indent under unless we are inside a string.
833 ;; Call indentation hook except when overridden by lisp-indent-offset
834 ;; or if the desired indentation has already been computed.
835 (let ((normal-indent (current-column)))
836 (cond ((elt state 3)
837 ;; Inside a string, don't change indentation.
838 nil)
839 ((and (integerp lisp-indent-offset) containing-sexp)
840 ;; Indent by constant offset
841 (goto-char containing-sexp)
842 (+ (current-column) lisp-indent-offset))
843 ;; in this case calculate-lisp-indent-last-sexp is not nil
844 (calculate-lisp-indent-last-sexp
846 ;; try to align the parameters of a known function
847 (and lisp-indent-function
848 (not retry)
849 (funcall lisp-indent-function indent-point state))
850 ;; If the function has no special alignment
851 ;; or it does not apply to this argument,
852 ;; try to align a constant-symbol under the last
853 ;; preceding constant symbol, if there is such one of
854 ;; the last 2 preceding symbols, in the previous
855 ;; uncommented line.
856 (and (save-excursion
857 (goto-char indent-point)
858 (skip-chars-forward " \t")
859 (looking-at ":"))
860 ;; The last sexp may not be at the indentation
861 ;; where it begins, so find that one, instead.
862 (save-excursion
863 (goto-char calculate-lisp-indent-last-sexp)
864 ;; Handle prefix characters and whitespace
865 ;; following an open paren. (Bug#1012)
866 (backward-prefix-chars)
867 (while (not (or (looking-back "^[ \t]*\\|([ \t]+"
868 (line-beginning-position))
869 (and containing-sexp
870 (>= (1+ containing-sexp) (point)))))
871 (forward-sexp -1)
872 (backward-prefix-chars))
873 (setq calculate-lisp-indent-last-sexp (point)))
874 (> calculate-lisp-indent-last-sexp
875 (save-excursion
876 (goto-char (1+ containing-sexp))
877 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
878 (point)))
879 (let ((parse-sexp-ignore-comments t)
880 indent)
881 (goto-char calculate-lisp-indent-last-sexp)
882 (or (and (looking-at ":")
883 (setq indent (current-column)))
884 (and (< (line-beginning-position)
885 (prog2 (backward-sexp) (point)))
886 (looking-at ":")
887 (setq indent (current-column))))
888 indent))
889 ;; another symbols or constants not preceded by a constant
890 ;; as defined above.
891 normal-indent))
892 ;; in this case calculate-lisp-indent-last-sexp is nil
893 (desired-indent)
895 normal-indent))))))
897 (defun lisp-indent-function (indent-point state)
898 "This function is the normal value of the variable `lisp-indent-function'.
899 The function `calculate-lisp-indent' calls this to determine
900 if the arguments of a Lisp function call should be indented specially.
902 INDENT-POINT is the position at which the line being indented begins.
903 Point is located at the point to indent under (for default indentation);
904 STATE is the `parse-partial-sexp' state for that position.
906 If the current line is in a call to a Lisp function that has a non-nil
907 property `lisp-indent-function' (or the deprecated `lisp-indent-hook'),
908 it specifies how to indent. The property value can be:
910 * `defun', meaning indent `defun'-style
911 (this is also the case if there is no property and the function
912 has a name that begins with \"def\", and three or more arguments);
914 * an integer N, meaning indent the first N arguments specially
915 (like ordinary function arguments), and then indent any further
916 arguments like a body;
918 * a function to call that returns the indentation (or nil).
919 `lisp-indent-function' calls this function with the same two arguments
920 that it itself received.
922 This function returns either the indentation to use, or nil if the
923 Lisp function does not specify a special indentation."
924 (let ((normal-indent (current-column)))
925 (goto-char (1+ (elt state 1)))
926 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
927 (if (and (elt state 2)
928 (not (looking-at "\\sw\\|\\s_")))
929 ;; car of form doesn't seem to be a symbol
930 (progn
931 (if (not (> (save-excursion (forward-line 1) (point))
932 calculate-lisp-indent-last-sexp))
933 (progn (goto-char calculate-lisp-indent-last-sexp)
934 (beginning-of-line)
935 (parse-partial-sexp (point)
936 calculate-lisp-indent-last-sexp 0 t)))
937 ;; Indent under the list or under the first sexp on the same
938 ;; line as calculate-lisp-indent-last-sexp. Note that first
939 ;; thing on that line has to be complete sexp since we are
940 ;; inside the innermost containing sexp.
941 (backward-prefix-chars)
942 (current-column))
943 (let ((function (buffer-substring (point)
944 (progn (forward-sexp 1) (point))))
945 method)
946 (setq method (or (function-get (intern-soft function)
947 'lisp-indent-function)
948 (get (intern-soft function) 'lisp-indent-hook)))
949 (cond ((or (eq method 'defun)
950 (and (null method)
951 (> (length function) 3)
952 (string-match "\\`def" function)))
953 (lisp-indent-defform state indent-point))
954 ((integerp method)
955 (lisp-indent-specform method state
956 indent-point normal-indent))
957 (method
958 (funcall method indent-point state)))))))
960 (defcustom lisp-body-indent 2
961 "Number of columns to indent the second line of a `(def...)' form."
962 :group 'lisp
963 :type 'integer)
964 (put 'lisp-body-indent 'safe-local-variable 'integerp)
966 (defun lisp-indent-specform (count state indent-point normal-indent)
967 (let ((containing-form-start (elt state 1))
968 (i count)
969 body-indent containing-form-column)
970 ;; Move to the start of containing form, calculate indentation
971 ;; to use for non-distinguished forms (> count), and move past the
972 ;; function symbol. lisp-indent-function guarantees that there is at
973 ;; least one word or symbol character following open paren of containing
974 ;; form.
975 (goto-char containing-form-start)
976 (setq containing-form-column (current-column))
977 (setq body-indent (+ lisp-body-indent containing-form-column))
978 (forward-char 1)
979 (forward-sexp 1)
980 ;; Now find the start of the last form.
981 (parse-partial-sexp (point) indent-point 1 t)
982 (while (and (< (point) indent-point)
983 (condition-case ()
984 (progn
985 (setq count (1- count))
986 (forward-sexp 1)
987 (parse-partial-sexp (point) indent-point 1 t))
988 (error nil))))
989 ;; Point is sitting on first character of last (or count) sexp.
990 (if (> count 0)
991 ;; A distinguished form. If it is the first or second form use double
992 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound
993 ;; to 2 (the default), this just happens to work the same with if as
994 ;; the older code, but it makes unwind-protect, condition-case,
995 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older,
996 ;; less hacked, behavior can be obtained by replacing below with
997 ;; (list normal-indent containing-form-start).
998 (if (<= (- i count) 1)
999 (list (+ containing-form-column (* 2 lisp-body-indent))
1000 containing-form-start)
1001 (list normal-indent containing-form-start))
1002 ;; A non-distinguished form. Use body-indent if there are no
1003 ;; distinguished forms and this is the first undistinguished form,
1004 ;; or if this is the first undistinguished form and the preceding
1005 ;; distinguished form has indentation at least as great as body-indent.
1006 (if (or (and (= i 0) (= count 0))
1007 (and (= count 0) (<= body-indent normal-indent)))
1008 body-indent
1009 normal-indent))))
1011 (defun lisp-indent-defform (state _indent-point)
1012 (goto-char (car (cdr state)))
1013 (forward-line 1)
1014 (if (> (point) (car (cdr (cdr state))))
1015 (progn
1016 (goto-char (car (cdr state)))
1017 (+ lisp-body-indent (current-column)))))
1020 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
1021 ;; like defun if the first form is placed on the next line, otherwise
1022 ;; it is indented like any other form (i.e. forms line up under first).
1024 (put 'autoload 'lisp-indent-function 'defun) ;Elisp
1025 (put 'progn 'lisp-indent-function 0)
1026 (put 'prog1 'lisp-indent-function 1)
1027 (put 'prog2 'lisp-indent-function 2)
1028 (put 'save-excursion 'lisp-indent-function 0) ;Elisp
1029 (put 'save-restriction 'lisp-indent-function 0) ;Elisp
1030 (put 'save-current-buffer 'lisp-indent-function 0) ;Elisp
1031 (put 'let 'lisp-indent-function 1)
1032 (put 'let* 'lisp-indent-function 1)
1033 (put 'while 'lisp-indent-function 1)
1034 (put 'if 'lisp-indent-function 2)
1035 (put 'catch 'lisp-indent-function 1)
1036 (put 'condition-case 'lisp-indent-function 2)
1037 (put 'handler-case 'lisp-indent-function 1) ;CL
1038 (put 'handler-bind 'lisp-indent-function 1) ;CL
1039 (put 'unwind-protect 'lisp-indent-function 1)
1040 (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
1042 (defun indent-sexp (&optional endpos)
1043 "Indent each line of the list starting just after point.
1044 If optional arg ENDPOS is given, indent each line, stopping when
1045 ENDPOS is encountered."
1046 (interactive)
1047 (let ((indent-stack (list nil))
1048 (next-depth 0)
1049 ;; If ENDPOS is non-nil, use nil as STARTING-POINT
1050 ;; so that calculate-lisp-indent will find the beginning of
1051 ;; the defun we are in.
1052 ;; If ENDPOS is nil, it is safe not to scan before point
1053 ;; since every line we indent is more deeply nested than point is.
1054 (starting-point (if endpos nil (point)))
1055 (last-point (point))
1056 last-depth bol outer-loop-done inner-loop-done state this-indent)
1057 (or endpos
1058 ;; Get error now if we don't have a complete sexp after point.
1059 (save-excursion (forward-sexp 1)))
1060 (save-excursion
1061 (setq outer-loop-done nil)
1062 (while (if endpos (< (point) endpos)
1063 (not outer-loop-done))
1064 (setq last-depth next-depth
1065 inner-loop-done nil)
1066 ;; Parse this line so we can learn the state
1067 ;; to indent the next line.
1068 ;; This inner loop goes through only once
1069 ;; unless a line ends inside a string.
1070 (while (and (not inner-loop-done)
1071 (not (setq outer-loop-done (eobp))))
1072 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
1073 nil nil state))
1074 (setq next-depth (car state))
1075 ;; If the line contains a comment other than the sort
1076 ;; that is indented like code,
1077 ;; indent it now with indent-for-comment.
1078 ;; Comments indented like code are right already.
1079 ;; In any case clear the in-comment flag in the state
1080 ;; because parse-partial-sexp never sees the newlines.
1081 (if (car (nthcdr 4 state))
1082 (progn (indent-for-comment)
1083 (end-of-line)
1084 (setcar (nthcdr 4 state) nil)))
1085 ;; If this line ends inside a string,
1086 ;; go straight to next line, remaining within the inner loop,
1087 ;; and turn off the \-flag.
1088 (if (car (nthcdr 3 state))
1089 (progn
1090 (forward-line 1)
1091 (setcar (nthcdr 5 state) nil))
1092 (setq inner-loop-done t)))
1093 (and endpos
1094 (<= next-depth 0)
1095 (progn
1096 (setq indent-stack (nconc indent-stack
1097 (make-list (- next-depth) nil))
1098 last-depth (- last-depth next-depth)
1099 next-depth 0)))
1100 (forward-line 1)
1101 ;; Decide whether to exit.
1102 (if endpos
1103 ;; If we have already reached the specified end,
1104 ;; give up and do not reindent this line.
1105 (if (<= endpos (point))
1106 (setq outer-loop-done t))
1107 ;; If no specified end, we are done if we have finished one sexp.
1108 (if (<= next-depth 0)
1109 (setq outer-loop-done t)))
1110 (unless outer-loop-done
1111 (while (> last-depth next-depth)
1112 (setq indent-stack (cdr indent-stack)
1113 last-depth (1- last-depth)))
1114 (while (< last-depth next-depth)
1115 (setq indent-stack (cons nil indent-stack)
1116 last-depth (1+ last-depth)))
1117 ;; Now indent the next line according
1118 ;; to what we learned from parsing the previous one.
1119 (setq bol (point))
1120 (skip-chars-forward " \t")
1121 ;; But not if the line is blank, or just a comment
1122 ;; (except for double-semi comments; indent them as usual).
1123 (if (or (eobp) (looking-at "\\s<\\|\n"))
1125 (if (and (car indent-stack)
1126 (>= (car indent-stack) 0))
1127 (setq this-indent (car indent-stack))
1128 (let ((val (calculate-lisp-indent
1129 (if (car indent-stack) (- (car indent-stack))
1130 starting-point))))
1131 (if (null val)
1132 (setq this-indent val)
1133 (if (integerp val)
1134 (setcar indent-stack
1135 (setq this-indent val))
1136 (setcar indent-stack (- (car (cdr val))))
1137 (setq this-indent (car val))))))
1138 (if (and this-indent (/= (current-column) this-indent))
1139 (progn (delete-region bol (point))
1140 (indent-to this-indent)))))
1141 (or outer-loop-done
1142 (setq outer-loop-done (= (point) last-point))
1143 (setq last-point (point)))))))
1145 (defun indent-pp-sexp (&optional arg)
1146 "Indent each line of the list starting just after point, or prettyprint it.
1147 A prefix argument specifies pretty-printing."
1148 (interactive "P")
1149 (if arg
1150 (save-excursion
1151 (save-restriction
1152 (narrow-to-region (point) (progn (forward-sexp 1) (point)))
1153 (pp-buffer)
1154 (goto-char (point-max))
1155 (if (eq (char-before) ?\n)
1156 (delete-char -1)))))
1157 (indent-sexp))
1159 ;;;; Lisp paragraph filling commands.
1161 (defcustom emacs-lisp-docstring-fill-column 65
1162 "Value of `fill-column' to use when filling a docstring.
1163 Any non-integer value means do not use a different value of
1164 `fill-column' when filling docstrings."
1165 :type '(choice (integer)
1166 (const :tag "Use the current `fill-column'" t))
1167 :group 'lisp)
1168 (put 'emacs-lisp-docstring-fill-column 'safe-local-variable
1169 (lambda (x) (or (eq x t) (integerp x))))
1171 (defun lisp-fill-paragraph (&optional justify)
1172 "Like \\[fill-paragraph], but handle Emacs Lisp comments and docstrings.
1173 If any of the current line is a comment, fill the comment or the
1174 paragraph of it that point is in, preserving the comment's indentation
1175 and initial semicolons."
1176 (interactive "P")
1177 (or (fill-comment-paragraph justify)
1178 ;; Since fill-comment-paragraph returned nil, that means we're not in
1179 ;; a comment: Point is on a program line; we are interested
1180 ;; particularly in docstring lines.
1182 ;; We bind `paragraph-start' and `paragraph-separate' temporarily. They
1183 ;; are buffer-local, but we avoid changing them so that they can be set
1184 ;; to make `forward-paragraph' and friends do something the user wants.
1186 ;; `paragraph-start': The `(' in the character alternative and the
1187 ;; left-singlequote plus `(' sequence after the \\| alternative prevent
1188 ;; sexps and backquoted sexps that follow a docstring from being filled
1189 ;; with the docstring. This setting has the consequence of inhibiting
1190 ;; filling many program lines that are not docstrings, which is sensible,
1191 ;; because the user probably asked to fill program lines by accident, or
1192 ;; expecting indentation (perhaps we should try to do indenting in that
1193 ;; case). The `;' and `:' stop the paragraph being filled at following
1194 ;; comment lines and at keywords (e.g., in `defcustom'). Left parens are
1195 ;; escaped to keep font-locking, filling, & paren matching in the source
1196 ;; file happy.
1198 ;; `paragraph-separate': A clever regexp distinguishes the first line of
1199 ;; a docstring and identifies it as a paragraph separator, so that it
1200 ;; won't be filled. (Since the first line of documentation stands alone
1201 ;; in some contexts, filling should not alter the contents the author has
1202 ;; chosen.) Only the first line of a docstring begins with whitespace
1203 ;; and a quotation mark and ends with a period or (rarely) a comma.
1205 ;; The `fill-column' is temporarily bound to
1206 ;; `emacs-lisp-docstring-fill-column' if that value is an integer.
1207 (let ((paragraph-start (concat paragraph-start
1208 "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)"))
1209 (paragraph-separate
1210 (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
1211 (fill-column (if (and (integerp emacs-lisp-docstring-fill-column)
1212 (derived-mode-p 'emacs-lisp-mode))
1213 emacs-lisp-docstring-fill-column
1214 fill-column)))
1215 (fill-paragraph justify))
1216 ;; Never return nil.
1219 (defun indent-code-rigidly (start end arg &optional nochange-regexp)
1220 "Indent all lines of code, starting in the region, sideways by ARG columns.
1221 Does not affect lines starting inside comments or strings, assuming that
1222 the start of the region is not inside them.
1224 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
1225 The last is a regexp which, if matched at the beginning of a line,
1226 means don't indent that line."
1227 (interactive "r\np")
1228 (let (state)
1229 (save-excursion
1230 (goto-char end)
1231 (setq end (point-marker))
1232 (goto-char start)
1233 (or (bolp)
1234 (setq state (parse-partial-sexp (point)
1235 (progn
1236 (forward-line 1) (point))
1237 nil nil state)))
1238 (while (< (point) end)
1239 (or (car (nthcdr 3 state))
1240 (and nochange-regexp
1241 (looking-at nochange-regexp))
1242 ;; If line does not start in string, indent it
1243 (let ((indent (current-indentation)))
1244 (delete-region (point) (progn (skip-chars-forward " \t") (point)))
1245 (or (eolp)
1246 (indent-to (max 0 (+ indent arg)) 0))))
1247 (setq state (parse-partial-sexp (point)
1248 (progn
1249 (forward-line 1) (point))
1250 nil nil state))))))
1252 (provide 'lisp-mode)
1254 ;;; lisp-mode.el ends here