Mercurial > emacs
annotate lisp/emacs-lisp/lisp-mode.el @ 12035:c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
(emacs-lisp-byte-compile): New function.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Tue, 30 May 1995 23:07:27 +0000 |
parents | bea3ee51b843 |
children | a4f383dd5adb |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands. |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
3 ;; Copyright (C) 1985 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
5 ;; Maintainer: FSF |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
6 ;; Keywords: lisp, languages |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
7 |
213 | 8 ;; This file is part of GNU Emacs. |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
213 | 13 ;; any later version. |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
24 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
25 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
26 ;; The base major mode for editing Lisp code (used also for Emacs Lisp). |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
27 ;; This mode is documented in the Emacs manual |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
28 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
29 ;;; Code: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
30 |
213 | 31 (defvar lisp-mode-syntax-table nil "") |
32 (defvar emacs-lisp-mode-syntax-table nil "") | |
33 (defvar lisp-mode-abbrev-table nil "") | |
34 | |
35 (if (not emacs-lisp-mode-syntax-table) | |
36 (let ((i 0)) | |
37 (setq emacs-lisp-mode-syntax-table (make-syntax-table)) | |
38 (while (< i ?0) | |
39 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
40 (setq i (1+ i))) | |
41 (setq i (1+ ?9)) | |
42 (while (< i ?A) | |
43 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
44 (setq i (1+ i))) | |
45 (setq i (1+ ?Z)) | |
46 (while (< i ?a) | |
47 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
48 (setq i (1+ i))) | |
49 (setq i (1+ ?z)) | |
50 (while (< i 128) | |
51 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
52 (setq i (1+ i))) | |
53 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table) | |
54 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table) | |
55 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table) | |
8325
2c8b3c988b10
(emacs-lisp-mode-syntax-table): Give RET the same syntax as newline.
Richard M. Stallman <rms@gnu.org>
parents:
7301
diff
changeset
|
56 ;; Give CR the same syntax as newline, for selective-display. |
2c8b3c988b10
(emacs-lisp-mode-syntax-table): Give RET the same syntax as newline.
Richard M. Stallman <rms@gnu.org>
parents:
7301
diff
changeset
|
57 (modify-syntax-entry ?\^m "> " emacs-lisp-mode-syntax-table) |
213 | 58 (modify-syntax-entry ?\; "< " emacs-lisp-mode-syntax-table) |
59 (modify-syntax-entry ?` "' " emacs-lisp-mode-syntax-table) | |
60 (modify-syntax-entry ?' "' " emacs-lisp-mode-syntax-table) | |
61 (modify-syntax-entry ?, "' " emacs-lisp-mode-syntax-table) | |
62 ;; Used to be singlequote; changed for flonums. | |
63 (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table) | |
64 (modify-syntax-entry ?# "' " emacs-lisp-mode-syntax-table) | |
65 (modify-syntax-entry ?\" "\" " emacs-lisp-mode-syntax-table) | |
66 (modify-syntax-entry ?\\ "\\ " emacs-lisp-mode-syntax-table) | |
67 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table) | |
68 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table) | |
69 (modify-syntax-entry ?\[ "(] " emacs-lisp-mode-syntax-table) | |
70 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table))) | |
71 | |
285 | 72 (if (not lisp-mode-syntax-table) |
73 (progn (setq lisp-mode-syntax-table | |
74 (copy-syntax-table emacs-lisp-mode-syntax-table)) | |
75 (modify-syntax-entry ?\| "\" " lisp-mode-syntax-table) | |
76 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table) | |
77 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table))) | |
78 | |
213 | 79 (define-abbrev-table 'lisp-mode-abbrev-table ()) |
80 | |
81 (defun lisp-mode-variables (lisp-syntax) | |
82 (cond (lisp-syntax | |
83 (set-syntax-table lisp-mode-syntax-table))) | |
84 (setq local-abbrev-table lisp-mode-abbrev-table) | |
85 (make-local-variable 'paragraph-start) | |
86 (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
87 (make-local-variable 'paragraph-separate) | |
88 (setq paragraph-separate paragraph-start) | |
89 (make-local-variable 'paragraph-ignore-fill-prefix) | |
90 (setq paragraph-ignore-fill-prefix t) | |
10628
04591bafb562
(shared-lisp-mode-map): Don't bind M-q.
Richard M. Stallman <rms@gnu.org>
parents:
10594
diff
changeset
|
91 (make-local-variable 'fill-paragraph-function) |
04591bafb562
(shared-lisp-mode-map): Don't bind M-q.
Richard M. Stallman <rms@gnu.org>
parents:
10594
diff
changeset
|
92 (setq fill-paragraph-function 'lisp-fill-paragraph) |
213 | 93 (make-local-variable 'indent-line-function) |
94 (setq indent-line-function 'lisp-indent-line) | |
95 (make-local-variable 'indent-region-function) | |
96 (setq indent-region-function 'lisp-indent-region) | |
97 (make-local-variable 'parse-sexp-ignore-comments) | |
98 (setq parse-sexp-ignore-comments t) | |
6076
4f76564fc2cd
(lisp-mode-variables): Set outline-regexp.
Richard M. Stallman <rms@gnu.org>
parents:
5103
diff
changeset
|
99 (make-local-variable 'outline-regexp) |
7301
edad1db699ab
(lisp-mode-variables): Added missing backslash to outline-regexp.
Richard M. Stallman <rms@gnu.org>
parents:
7170
diff
changeset
|
100 (setq outline-regexp ";;; \\|(....") |
213 | 101 (make-local-variable 'comment-start) |
102 (setq comment-start ";") | |
103 (make-local-variable 'comment-start-skip) | |
7170
3b452d97f111
(lisp-mode-variables): Undo previous change.
Karl Heuer <kwzh@gnu.org>
parents:
7079
diff
changeset
|
104 (setq comment-start-skip ";+ *") |
213 | 105 (make-local-variable 'comment-column) |
106 (setq comment-column 40) | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
107 (make-local-variable 'comment-indent-function) |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2232
diff
changeset
|
108 (setq comment-indent-function 'lisp-comment-indent)) |
213 | 109 |
110 (defvar shared-lisp-mode-map () | |
111 "Keymap for commands shared by all sorts of Lisp modes.") | |
112 | |
113 (if shared-lisp-mode-map | |
114 () | |
115 (setq shared-lisp-mode-map (make-sparse-keymap)) | |
116 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp) | |
117 (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify) | |
118 (define-key shared-lisp-mode-map "\t" 'lisp-indent-line)) | |
119 | |
120 (defvar emacs-lisp-mode-map () | |
121 "Keymap for Emacs Lisp mode. | |
122 All commands in shared-lisp-mode-map are inherited by this map.") | |
123 | |
124 (if emacs-lisp-mode-map | |
125 () | |
12035
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
126 (let ((map (make-sparse-keymap "Emacs-Lisp"))) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
127 (setq emacs-lisp-mode-map |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
128 (nconc (make-sparse-keymap) shared-lisp-mode-map)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
129 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
130 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
131 (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
132 (define-key emacs-lisp-mode-map [menu-bar emacs-lisp] |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
133 (cons "Emacs-Lisp" map)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
134 (define-key map [edebug-defun] |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
135 '("Instrument Function for Debugging" . edebug-defun)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
136 (define-key map [byte-recompile] |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
137 '("Byte-recompile Directory..." . byte-recompile-directory)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
138 (define-key map [byte-compile] |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
139 '("Byte-compile This File" . emacs-lisp-byte-compile)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
140 (define-key map [separator-eval] '("--")) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
141 (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
142 (define-key map [eval-region] '("Evaluate Region" . eval-region)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
143 (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
144 (define-key map [separator-format] '("--")) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
145 (define-key map [comment-region] '("Comment Out Region" . comment-region)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
146 (define-key map [indent-region] '("Indent Region" . indent-region)) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
147 (define-key map [indent-line] '("Indent Line" . lisp-indent-line)))) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
148 |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
149 (defun emacs-lisp-byte-compile () |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
150 "Byte compile the file containing the current buffer." |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
151 (interactive) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
152 (if buffer-file-name |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
153 (byte-compile-file buffer-file-name) |
c3747e64cff0
(emacs-lisp-mode-map): Add a menu.
Karl Heuer <kwzh@gnu.org>
parents:
10968
diff
changeset
|
154 (error "The buffer must be saved in a file first."))) |
213 | 155 |
156 (defun emacs-lisp-mode () | |
157 "Major mode for editing Lisp code to run in Emacs. | |
158 Commands: | |
159 Delete converts tabs to spaces as it moves back. | |
160 Blank lines separate paragraphs. Semicolons start comments. | |
161 \\{emacs-lisp-mode-map} | |
162 Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
163 if that value is non-nil." | |
164 (interactive) | |
165 (kill-all-local-variables) | |
166 (use-local-map emacs-lisp-mode-map) | |
167 (set-syntax-table emacs-lisp-mode-syntax-table) | |
168 (setq major-mode 'emacs-lisp-mode) | |
169 (setq mode-name "Emacs-Lisp") | |
170 (lisp-mode-variables nil) | |
171 (run-hooks 'emacs-lisp-mode-hook)) | |
172 | |
173 (defvar lisp-mode-map () | |
174 "Keymap for ordinary Lisp mode. | |
175 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
176 | |
177 (if lisp-mode-map | |
178 () | |
179 (setq lisp-mode-map | |
180 (nconc (make-sparse-keymap) shared-lisp-mode-map)) | |
5103
40b068fb4c37
(lisp-eval-defun): Renamed from lisp-send-defun.
Richard M. Stallman <rms@gnu.org>
parents:
3638
diff
changeset
|
181 (define-key lisp-mode-map "\e\C-x" 'lisp-eval-defun) |
40b068fb4c37
(lisp-eval-defun): Renamed from lisp-send-defun.
Richard M. Stallman <rms@gnu.org>
parents:
3638
diff
changeset
|
182 (define-key lisp-mode-map "\C-c\C-z" 'run-lisp)) |
213 | 183 |
184 (defun lisp-mode () | |
185 "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp. | |
186 Commands: | |
187 Delete converts tabs to spaces as it moves back. | |
188 Blank lines separate paragraphs. Semicolons start comments. | |
189 \\{lisp-mode-map} | |
190 Note that `run-lisp' may be used either to start an inferior Lisp job | |
191 or to switch back to an existing one. | |
192 | |
193 Entry to this mode calls the value of `lisp-mode-hook' | |
194 if that value is non-nil." | |
195 (interactive) | |
196 (kill-all-local-variables) | |
197 (use-local-map lisp-mode-map) | |
198 (setq major-mode 'lisp-mode) | |
199 (setq mode-name "Lisp") | |
200 (lisp-mode-variables t) | |
201 (set-syntax-table lisp-mode-syntax-table) | |
202 (run-hooks 'lisp-mode-hook)) | |
203 | |
204 ;; This will do unless shell.el is loaded. | |
5103
40b068fb4c37
(lisp-eval-defun): Renamed from lisp-send-defun.
Richard M. Stallman <rms@gnu.org>
parents:
3638
diff
changeset
|
205 (defun lisp-eval-defun nil |
213 | 206 "Send the current defun to the Lisp process made by \\[run-lisp]." |
207 (interactive) | |
208 (error "Process lisp does not exist")) | |
209 | |
210 (defvar lisp-interaction-mode-map () | |
211 "Keymap for Lisp Interaction moe. | |
212 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
213 | |
214 (if lisp-interaction-mode-map | |
215 () | |
216 (setq lisp-interaction-mode-map | |
217 (nconc (make-sparse-keymap) shared-lisp-mode-map)) | |
218 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun) | |
878
5b1c5b4286e7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
219 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol) |
213 | 220 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp)) |
221 | |
222 (defun lisp-interaction-mode () | |
223 "Major mode for typing and evaluating Lisp forms. | |
224 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
225 before point, and prints its value into the buffer, advancing point. | |
226 | |
227 Commands: | |
228 Delete converts tabs to spaces as it moves back. | |
229 Paragraphs are separated only by blank lines. | |
230 Semicolons start comments. | |
231 \\{lisp-interaction-mode-map} | |
232 Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
233 if that value is non-nil." | |
234 (interactive) | |
235 (kill-all-local-variables) | |
236 (use-local-map lisp-interaction-mode-map) | |
237 (setq major-mode 'lisp-interaction-mode) | |
238 (setq mode-name "Lisp Interaction") | |
239 (set-syntax-table emacs-lisp-mode-syntax-table) | |
240 (lisp-mode-variables nil) | |
241 (run-hooks 'lisp-interaction-mode-hook)) | |
242 | |
248 | 243 (defun eval-print-last-sexp () |
213 | 244 "Evaluate sexp before point; print value into current buffer." |
248 | 245 (interactive) |
422 | 246 (let ((standard-output (current-buffer))) |
247 (terpri) | |
248 (eval-last-sexp t) | |
249 (terpri))) | |
213 | 250 |
2450
17d258d8e8e4
(eval-defun): Rename argument to avoid collision.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
251 (defun eval-last-sexp (eval-last-sexp-arg-internal) |
213 | 252 "Evaluate sexp before point; print value in minibuffer. |
253 With argument, print output into current buffer." | |
254 (interactive "P") | |
2450
17d258d8e8e4
(eval-defun): Rename argument to avoid collision.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
255 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)) |
1163 | 256 (opoint (point))) |
249 | 257 (prin1 (let ((stab (syntax-table))) |
258 (eval (unwind-protect | |
259 (save-excursion | |
260 (set-syntax-table emacs-lisp-mode-syntax-table) | |
261 (forward-sexp -1) | |
1163 | 262 (save-restriction |
263 (narrow-to-region (point-min) opoint) | |
264 (read (current-buffer)))) | |
249 | 265 (set-syntax-table stab))))))) |
213 | 266 |
2450
17d258d8e8e4
(eval-defun): Rename argument to avoid collision.
Richard M. Stallman <rms@gnu.org>
parents:
2307
diff
changeset
|
267 (defun eval-defun (eval-defun-arg-internal) |
248 | 268 "Evaluate defun that point is in or before. |
269 Print value in minibuffer. | |
270 With argument, insert value in current buffer after the defun." | |
213 | 271 (interactive "P") |
10968
bea3ee51b843
(eval-defun): Convert defvar to defconst.
Richard M. Stallman <rms@gnu.org>
parents:
10629
diff
changeset
|
272 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t)) |
bea3ee51b843
(eval-defun): Convert defvar to defconst.
Richard M. Stallman <rms@gnu.org>
parents:
10629
diff
changeset
|
273 (form (save-excursion |
bea3ee51b843
(eval-defun): Convert defvar to defconst.
Richard M. Stallman <rms@gnu.org>
parents:
10629
diff
changeset
|
274 (end-of-defun) |
bea3ee51b843
(eval-defun): Convert defvar to defconst.
Richard M. Stallman <rms@gnu.org>
parents:
10629
diff
changeset
|
275 (beginning-of-defun) |
bea3ee51b843
(eval-defun): Convert defvar to defconst.
Richard M. Stallman <rms@gnu.org>
parents:
10629
diff
changeset
|
276 (read (current-buffer))))) |
bea3ee51b843
(eval-defun): Convert defvar to defconst.
Richard M. Stallman <rms@gnu.org>
parents:
10629
diff
changeset
|
277 (if (eq (car form) 'defvar) |
bea3ee51b843
(eval-defun): Convert defvar to defconst.
Richard M. Stallman <rms@gnu.org>
parents:
10629
diff
changeset
|
278 (setq form (cons 'defconst (cdr form)))) |
bea3ee51b843
(eval-defun): Convert defvar to defconst.
Richard M. Stallman <rms@gnu.org>
parents:
10629
diff
changeset
|
279 (prin1 (eval form)))) |
213 | 280 |
281 (defun lisp-comment-indent () | |
282 (if (looking-at "\\s<\\s<\\s<") | |
283 (current-column) | |
284 (if (looking-at "\\s<\\s<") | |
285 (let ((tem (calculate-lisp-indent))) | |
286 (if (listp tem) (car tem) tem)) | |
287 (skip-chars-backward " \t") | |
288 (max (if (bolp) 0 (1+ (current-column))) | |
289 comment-column)))) | |
290 | |
291 (defconst lisp-indent-offset nil "") | |
292 (defconst lisp-indent-function 'lisp-indent-function "") | |
293 | |
294 (defun lisp-indent-line (&optional whole-exp) | |
295 "Indent current line as Lisp code. | |
296 With argument, indent any additional lines of the same expression | |
297 rigidly along with this one." | |
298 (interactive "P") | |
299 (let ((indent (calculate-lisp-indent)) shift-amt beg end | |
300 (pos (- (point-max) (point)))) | |
301 (beginning-of-line) | |
302 (setq beg (point)) | |
303 (skip-chars-forward " \t") | |
304 (if (looking-at "\\s<\\s<\\s<") | |
305 ;; Don't alter indentation of a ;;; comment line. | |
677
7a9b4ea68565
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
659
diff
changeset
|
306 (goto-char (- (point-max) pos)) |
213 | 307 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) |
308 ;; Single-semicolon comment lines should be indented | |
309 ;; as comment lines, not as code. | |
310 (progn (indent-for-comment) (forward-char -1)) | |
311 (if (listp indent) (setq indent (car indent))) | |
312 (setq shift-amt (- indent (current-column))) | |
313 (if (zerop shift-amt) | |
314 nil | |
315 (delete-region beg (point)) | |
316 (indent-to indent))) | |
317 ;; If initial point was within line's indentation, | |
318 ;; position after the indentation. Else stay at same point in text. | |
319 (if (> (- (point-max) pos) (point)) | |
320 (goto-char (- (point-max) pos))) | |
321 ;; If desired, shift remaining lines of expression the same amount. | |
322 (and whole-exp (not (zerop shift-amt)) | |
323 (save-excursion | |
324 (goto-char beg) | |
325 (forward-sexp 1) | |
326 (setq end (point)) | |
327 (goto-char beg) | |
328 (forward-line 1) | |
329 (setq beg (point)) | |
330 (> end beg)) | |
331 (indent-code-rigidly beg end shift-amt))))) | |
332 | |
9424
738999b0296f
Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
9423
diff
changeset
|
333 (defvar calculate-lisp-indent-last-sexp) |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
334 |
213 | 335 (defun calculate-lisp-indent (&optional parse-start) |
336 "Return appropriate indentation for current line as Lisp code. | |
337 In usual case returns an integer: the column to indent to. | |
338 Can instead return a list, whose car is the column to indent to. | |
339 This means that following lines at the same level of indentation | |
340 should not necessarily be indented the same way. | |
341 The second element of the list is the buffer position | |
342 of the start of the containing expression." | |
343 (save-excursion | |
344 (beginning-of-line) | |
345 (let ((indent-point (point)) | |
346 state paren-depth | |
347 ;; setting this to a number inhibits calling hook | |
348 (desired-indent nil) | |
349 (retry t) | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
350 calculate-lisp-indent-last-sexp containing-sexp) |
213 | 351 (if parse-start |
352 (goto-char parse-start) | |
353 (beginning-of-defun)) | |
354 ;; Find outermost containing sexp | |
355 (while (< (point) indent-point) | |
356 (setq state (parse-partial-sexp (point) indent-point 0))) | |
357 ;; Find innermost containing sexp | |
358 (while (and retry | |
359 state | |
360 (> (setq paren-depth (elt state 0)) 0)) | |
361 (setq retry nil) | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
362 (setq calculate-lisp-indent-last-sexp (elt state 2)) |
213 | 363 (setq containing-sexp (elt state 1)) |
364 ;; Position following last unclosed open. | |
365 (goto-char (1+ containing-sexp)) | |
366 ;; Is there a complete sexp since then? | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
367 (if (and calculate-lisp-indent-last-sexp |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
368 (> calculate-lisp-indent-last-sexp (point))) |
213 | 369 ;; Yes, but is there a containing sexp after that? |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
370 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
371 indent-point 0))) |
213 | 372 (if (setq retry (car (cdr peek))) (setq state peek))))) |
373 (if retry | |
374 nil | |
375 ;; Innermost containing sexp found | |
376 (goto-char (1+ containing-sexp)) | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
377 (if (not calculate-lisp-indent-last-sexp) |
213 | 378 ;; indent-point immediately follows open paren. |
379 ;; Don't call hook. | |
380 (setq desired-indent (current-column)) | |
381 ;; Find the start of first element of containing sexp. | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
382 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
213 | 383 (cond ((looking-at "\\s(") |
384 ;; First element of containing sexp is a list. | |
385 ;; Indent under that list. | |
386 ) | |
387 ((> (save-excursion (forward-line 1) (point)) | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
388 calculate-lisp-indent-last-sexp) |
213 | 389 ;; This is the first line to start within the containing sexp. |
390 ;; It's almost certainly a function call. | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
391 (if (= (point) calculate-lisp-indent-last-sexp) |
213 | 392 ;; Containing sexp has nothing before this line |
393 ;; except the first element. Indent under that element. | |
394 nil | |
395 ;; Skip the first element, find start of second (the first | |
396 ;; argument of the function call) and indent under. | |
397 (progn (forward-sexp 1) | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
398 (parse-partial-sexp (point) |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
399 calculate-lisp-indent-last-sexp |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
400 0 t))) |
213 | 401 (backward-prefix-chars)) |
402 (t | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
403 ;; Indent beneath first sexp on same line as |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
404 ;; calculate-lisp-indent-last-sexp. Again, it's |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
405 ;; almost certainly a function call. |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
406 (goto-char calculate-lisp-indent-last-sexp) |
213 | 407 (beginning-of-line) |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
408 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
409 0 t) |
213 | 410 (backward-prefix-chars))))) |
411 ;; Point is at the point to indent under unless we are inside a string. | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3241
diff
changeset
|
412 ;; Call indentation hook except when overridden by lisp-indent-offset |
213 | 413 ;; or if the desired indentation has already been computed. |
414 (let ((normal-indent (current-column))) | |
415 (cond ((elt state 3) | |
416 ;; Inside a string, don't change indentation. | |
417 (goto-char indent-point) | |
418 (skip-chars-forward " \t") | |
419 (current-column)) | |
420 ((and (integerp lisp-indent-offset) containing-sexp) | |
421 ;; Indent by constant offset | |
422 (goto-char containing-sexp) | |
423 (+ (current-column) lisp-indent-offset)) | |
424 (desired-indent) | |
425 ((and (boundp 'lisp-indent-function) | |
426 lisp-indent-function | |
427 (not retry)) | |
428 (or (funcall lisp-indent-function indent-point state) | |
429 normal-indent)) | |
430 (t | |
214 | 431 normal-indent)))))) |
213 | 432 |
433 (defun lisp-indent-function (indent-point state) | |
434 (let ((normal-indent (current-column))) | |
435 (goto-char (1+ (elt state 1))) | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
436 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) |
213 | 437 (if (and (elt state 2) |
438 (not (looking-at "\\sw\\|\\s_"))) | |
439 ;; car of form doesn't seem to be a a symbol | |
440 (progn | |
441 (if (not (> (save-excursion (forward-line 1) (point)) | |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
442 calculate-lisp-indent-last-sexp)) |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
443 (progn (goto-char calculate-lisp-indent-last-sexp) |
213 | 444 (beginning-of-line) |
9423
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
445 (parse-partial-sexp (point) |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
446 calculate-lisp-indent-last-sexp 0 t))) |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
447 ;; Indent under the list or under the first sexp on the same |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
448 ;; line as calculate-lisp-indent-last-sexp. Note that first |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
449 ;; thing on that line has to be complete sexp since we are |
9076245a9fed
(calculate-lisp-indent): Local var
Richard M. Stallman <rms@gnu.org>
parents:
9415
diff
changeset
|
450 ;; inside the innermost containing sexp. |
213 | 451 (backward-prefix-chars) |
452 (current-column)) | |
453 (let ((function (buffer-substring (point) | |
454 (progn (forward-sexp 1) (point)))) | |
455 method) | |
3638
45169f86d3a4
(lisp-indent-function): Look for either
Richard M. Stallman <rms@gnu.org>
parents:
3591
diff
changeset
|
456 (setq method (or (get (intern-soft function) 'lisp-indent-function) |
45169f86d3a4
(lisp-indent-function): Look for either
Richard M. Stallman <rms@gnu.org>
parents:
3591
diff
changeset
|
457 (get (intern-soft function) 'lisp-indent-hook))) |
213 | 458 (cond ((or (eq method 'defun) |
459 (and (null method) | |
460 (> (length function) 3) | |
461 (string-match "\\`def" function))) | |
462 (lisp-indent-defform state indent-point)) | |
463 ((integerp method) | |
464 (lisp-indent-specform method state | |
465 indent-point normal-indent)) | |
466 (method | |
467 (funcall method state indent-point))))))) | |
468 | |
2933
420dfaaacfc5
(lisp-body-indent): Add doc.
Richard M. Stallman <rms@gnu.org>
parents:
2450
diff
changeset
|
469 (defconst lisp-body-indent 2 |
420dfaaacfc5
(lisp-body-indent): Add doc.
Richard M. Stallman <rms@gnu.org>
parents:
2450
diff
changeset
|
470 "Number of columns to indent the second line of a `(def...)' form.") |
213 | 471 |
472 (defun lisp-indent-specform (count state indent-point normal-indent) | |
473 (let ((containing-form-start (elt state 1)) | |
474 (i count) | |
475 body-indent containing-form-column) | |
476 ;; Move to the start of containing form, calculate indentation | |
477 ;; to use for non-distinguished forms (> count), and move past the | |
478 ;; function symbol. lisp-indent-function guarantees that there is at | |
479 ;; least one word or symbol character following open paren of containing | |
480 ;; form. | |
481 (goto-char containing-form-start) | |
482 (setq containing-form-column (current-column)) | |
483 (setq body-indent (+ lisp-body-indent containing-form-column)) | |
484 (forward-char 1) | |
485 (forward-sexp 1) | |
486 ;; Now find the start of the last form. | |
487 (parse-partial-sexp (point) indent-point 1 t) | |
488 (while (and (< (point) indent-point) | |
489 (condition-case () | |
490 (progn | |
491 (setq count (1- count)) | |
492 (forward-sexp 1) | |
493 (parse-partial-sexp (point) indent-point 1 t)) | |
494 (error nil)))) | |
495 ;; Point is sitting on first character of last (or count) sexp. | |
496 (if (> count 0) | |
497 ;; A distinguished form. If it is the first or second form use double | |
498 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
499 ;; to 2 (the default), this just happens to work the same with if as | |
500 ;; the older code, but it makes unwind-protect, condition-case, | |
501 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
502 ;; less hacked, behavior can be obtained by replacing below with | |
503 ;; (list normal-indent containing-form-start). | |
504 (if (<= (- i count) 1) | |
505 (list (+ containing-form-column (* 2 lisp-body-indent)) | |
506 containing-form-start) | |
507 (list normal-indent containing-form-start)) | |
508 ;; A non-distinguished form. Use body-indent if there are no | |
509 ;; distinguished forms and this is the first undistinguished form, | |
510 ;; or if this is the first undistinguished form and the preceding | |
511 ;; distinguished form has indentation at least as great as body-indent. | |
512 (if (or (and (= i 0) (= count 0)) | |
513 (and (= count 0) (<= body-indent normal-indent))) | |
514 body-indent | |
515 normal-indent)))) | |
516 | |
517 (defun lisp-indent-defform (state indent-point) | |
518 (goto-char (car (cdr state))) | |
519 (forward-line 1) | |
520 (if (> (point) (car (cdr (cdr state)))) | |
521 (progn | |
522 (goto-char (car (cdr state))) | |
523 (+ lisp-body-indent (current-column))))) | |
524 | |
525 | |
526 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented | |
527 ;; like defun if the first form is placed on the next line, otherwise | |
528 ;; it is indented like any other form (i.e. forms line up under first). | |
529 | |
530 (put 'lambda 'lisp-indent-function 'defun) | |
531 (put 'autoload 'lisp-indent-function 'defun) | |
532 (put 'progn 'lisp-indent-function 0) | |
533 (put 'prog1 'lisp-indent-function 1) | |
534 (put 'prog2 'lisp-indent-function 2) | |
535 (put 'save-excursion 'lisp-indent-function 0) | |
536 (put 'save-window-excursion 'lisp-indent-function 0) | |
537 (put 'save-restriction 'lisp-indent-function 0) | |
1163 | 538 (put 'save-match-data 'lisp-indent-function 0) |
213 | 539 (put 'let 'lisp-indent-function 1) |
540 (put 'let* 'lisp-indent-function 1) | |
541 (put 'while 'lisp-indent-function 1) | |
542 (put 'if 'lisp-indent-function 2) | |
543 (put 'catch 'lisp-indent-function 1) | |
544 (put 'condition-case 'lisp-indent-function 2) | |
545 (put 'unwind-protect 'lisp-indent-function 1) | |
546 (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
547 | |
548 (defun indent-sexp (&optional endpos) | |
549 "Indent each line of the list starting just after point. | |
550 If optional arg ENDPOS is given, indent each line, stopping when | |
551 ENDPOS is encountered." | |
552 (interactive) | |
727 | 553 (let ((indent-stack (list nil)) |
554 (next-depth 0) | |
10594
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
555 ;; If ENDPOS is non-nil, use nil as STARTING-POINT |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
556 ;; so that calculate-lisp-indent will find the beginning of |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
557 ;; the defun we are in. |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
558 ;; If ENDPOS is nil, it is safe not to scan before point |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
559 ;; since every line we indent is more deeply nested than point is. |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
560 (starting-point (if endpos nil (point))) |
727 | 561 (last-point (point)) |
562 last-depth bol outer-loop-done inner-loop-done state this-indent) | |
10594
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
563 (or endpos |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
564 ;; Get error now if we don't have a complete sexp after point. |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
565 (save-excursion (forward-sexp 1))) |
213 | 566 (save-excursion |
567 (setq outer-loop-done nil) | |
568 (while (if endpos (< (point) endpos) | |
569 (not outer-loop-done)) | |
570 (setq last-depth next-depth | |
571 inner-loop-done nil) | |
572 ;; Parse this line so we can learn the state | |
573 ;; to indent the next line. | |
574 ;; This inner loop goes through only once | |
575 ;; unless a line ends inside a string. | |
576 (while (and (not inner-loop-done) | |
577 (not (setq outer-loop-done (eobp)))) | |
578 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
579 nil nil state)) | |
580 (setq next-depth (car state)) | |
581 ;; If the line contains a comment other than the sort | |
582 ;; that is indented like code, | |
583 ;; indent it now with indent-for-comment. | |
584 ;; Comments indented like code are right already. | |
585 ;; In any case clear the in-comment flag in the state | |
586 ;; because parse-partial-sexp never sees the newlines. | |
587 (if (car (nthcdr 4 state)) | |
588 (progn (indent-for-comment) | |
589 (end-of-line) | |
590 (setcar (nthcdr 4 state) nil))) | |
591 ;; If this line ends inside a string, | |
592 ;; go straight to next line, remaining within the inner loop, | |
593 ;; and turn off the \-flag. | |
594 (if (car (nthcdr 3 state)) | |
595 (progn | |
596 (forward-line 1) | |
597 (setcar (nthcdr 5 state) nil)) | |
598 (setq inner-loop-done t))) | |
599 (and endpos | |
727 | 600 (<= next-depth 0) |
601 (progn | |
602 (setq indent-stack (append indent-stack | |
603 (make-list (- next-depth) nil)) | |
604 last-depth (- last-depth next-depth) | |
605 next-depth 0))) | |
10594
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
606 (or outer-loop-done endpos |
213 | 607 (setq outer-loop-done (<= next-depth 0))) |
608 (if outer-loop-done | |
3241
6c9b5f6dca70
(indent-sexp): Even if outer-loop-done is t, still move down one line.
Richard M. Stallman <rms@gnu.org>
parents:
2933
diff
changeset
|
609 (forward-line 1) |
213 | 610 (while (> last-depth next-depth) |
611 (setq indent-stack (cdr indent-stack) | |
612 last-depth (1- last-depth))) | |
613 (while (< last-depth next-depth) | |
614 (setq indent-stack (cons nil indent-stack) | |
615 last-depth (1+ last-depth))) | |
616 ;; Now go to the next line and indent it according | |
617 ;; to what we learned from parsing the previous one. | |
618 (forward-line 1) | |
619 (setq bol (point)) | |
620 (skip-chars-forward " \t") | |
621 ;; But not if the line is blank, or just a comment | |
622 ;; (except for double-semi comments; indent them as usual). | |
623 (if (or (eobp) (looking-at "\\s<\\|\n")) | |
624 nil | |
625 (if (and (car indent-stack) | |
626 (>= (car indent-stack) 0)) | |
627 (setq this-indent (car indent-stack)) | |
628 (let ((val (calculate-lisp-indent | |
629 (if (car indent-stack) (- (car indent-stack)) | |
727 | 630 starting-point)))) |
213 | 631 (if (integerp val) |
632 (setcar indent-stack | |
633 (setq this-indent val)) | |
634 (setcar indent-stack (- (car (cdr val)))) | |
635 (setq this-indent (car val))))) | |
636 (if (/= (current-column) this-indent) | |
637 (progn (delete-region bol (point)) | |
638 (indent-to this-indent))))) | |
639 (or outer-loop-done | |
640 (setq outer-loop-done (= (point) last-point)) | |
641 (setq last-point (point))))))) | |
642 | |
643 ;; Indent every line whose first char is between START and END inclusive. | |
644 (defun lisp-indent-region (start end) | |
645 (save-excursion | |
646 (let ((endmark (copy-marker end))) | |
10594
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
647 (goto-char start) |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
648 (and (bolp) (not (eolp)) |
aadef46f00f7
(lisp-indent-region): Set endmark before indenting first line.
Richard M. Stallman <rms@gnu.org>
parents:
9424
diff
changeset
|
649 (lisp-indent-line)) |
213 | 650 (indent-sexp endmark) |
651 (set-marker endmark nil)))) | |
652 | |
1865
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
653 ;;;; Lisp paragraph filling commands. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
654 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
655 (defun lisp-fill-paragraph (&optional justify) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
656 "Like \\[fill-paragraph], but handle Emacs Lisp comments. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
657 If any of the current line is a comment, fill the comment or the |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
658 paragraph of it that point is in, preserving the comment's indentation |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
659 and initial semicolons." |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
660 (interactive "P") |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
661 (let ( |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
662 ;; Non-nil if the current line contains a comment. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
663 has-comment |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
664 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
665 ;; If has-comment, the appropriate fill-prefix for the comment. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
666 comment-fill-prefix |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
667 ) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
668 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
669 ;; Figure out what kind of comment we are looking at. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
670 (save-excursion |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
671 (beginning-of-line) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
672 (cond |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
673 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
674 ;; A line with nothing but a comment on it? |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
675 ((looking-at "[ \t]*;[; \t]*") |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
676 (setq has-comment t |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
677 comment-fill-prefix (buffer-substring (match-beginning 0) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
678 (match-end 0)))) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
679 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
680 ;; A line with some code, followed by a comment? Remember that the |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
681 ;; semi which starts the comment shouldn't be part of a string or |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
682 ;; character. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
683 ((progn |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
684 (while (not (looking-at ";\\|$")) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
685 (skip-chars-forward "^;\n\"\\\\?") |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
686 (cond |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
687 ((eq (char-after (point)) ?\\) (forward-char 2)) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
688 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
689 (looking-at ";+[\t ]*")) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
690 (setq has-comment t) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
691 (setq comment-fill-prefix |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
692 (concat (make-string (current-column) ? ) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
693 (buffer-substring (match-beginning 0) (match-end 0))))))) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
694 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
695 (if (not has-comment) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
696 (fill-paragraph justify) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
697 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
698 ;; Narrow to include only the comment, and then fill the region. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
699 (save-restriction |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
700 (narrow-to-region |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
701 ;; Find the first line we should include in the region to fill. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
702 (save-excursion |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
703 (while (and (zerop (forward-line -1)) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
704 (looking-at "^[ \t]*;"))) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
705 ;; We may have gone to far. Go forward again. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
706 (or (looking-at "^[ \t]*;") |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
707 (forward-line 1)) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
708 (point)) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
709 ;; Find the beginning of the first line past the region to fill. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
710 (save-excursion |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
711 (while (progn (forward-line 1) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
712 (looking-at "^[ \t]*;"))) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
713 (point))) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
714 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
715 ;; Lines with only semicolons on them can be paragraph boundaries. |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
716 (let ((paragraph-start (concat paragraph-start "\\|^[ \t;]*$")) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
717 (paragraph-separate (concat paragraph-start "\\|^[ \t;]*$")) |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
718 (fill-prefix comment-fill-prefix)) |
10629
2c9bdcab7052
(lisp-fill-paragraph): Return t.
Richard M. Stallman <rms@gnu.org>
parents:
10628
diff
changeset
|
719 (fill-paragraph justify)))) |
2c9bdcab7052
(lisp-fill-paragraph): Return t.
Richard M. Stallman <rms@gnu.org>
parents:
10628
diff
changeset
|
720 t)) |
1865
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
721 |
05be3e0c082f
* lisp-mode.el (lisp-fill-paragraph): New function.
Jim Blandy <jimb@redhat.com>
parents:
1163
diff
changeset
|
722 |
213 | 723 (defun indent-code-rigidly (start end arg &optional nochange-regexp) |
724 "Indent all lines of code, starting in the region, sideways by ARG columns. | |
725 Does not affect lines starting inside comments or strings, assuming that | |
726 the start of the region is not inside them. | |
727 | |
728 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
729 The last is a regexp which, if matched at the beginning of a line, | |
730 means don't indent that line." | |
731 (interactive "r\np") | |
732 (let (state) | |
733 (save-excursion | |
734 (goto-char end) | |
735 (setq end (point-marker)) | |
736 (goto-char start) | |
737 (or (bolp) | |
738 (setq state (parse-partial-sexp (point) | |
739 (progn | |
740 (forward-line 1) (point)) | |
741 nil nil state))) | |
742 (while (< (point) end) | |
743 (or (car (nthcdr 3 state)) | |
744 (and nochange-regexp | |
745 (looking-at nochange-regexp)) | |
746 ;; If line does not start in string, indent it | |
747 (let ((indent (current-indentation))) | |
748 (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
749 (or (eolp) | |
750 (indent-to (max 0 (+ indent arg)) 0)))) | |
751 (setq state (parse-partial-sexp (point) | |
752 (progn | |
753 (forward-line 1) (point)) | |
754 nil nil state)))))) | |
584 | 755 |
756 (provide 'lisp-mode) | |
757 | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
758 ;;; lisp-mode.el ends here |