Mercurial > hg > xemacs-beta
annotate lisp/lisp-mode.el @ 5561:9a93bc90b3bd
Add a defsetf for get-char-table, necessary for the tests in the last commit.
lisp/ChangeLog addition:
2011-09-04 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (get-char-table): Add a defsetf for this.
| author | Aidan Kehoe <kehoea@parhasard.net> |
|---|---|
| date | Sun, 04 Sep 2011 20:35:31 +0100 |
| parents | 308d34e9f07d |
| children | 3bc58dc9d688 |
| rev | line source |
|---|---|
| 428 | 1 ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands. |
| 2 | |
| 3 ;; Copyright (C) 1985, 1996, 1997 Free Software Foundation, Inc. | |
| 442 | 4 ;; Copyright (C) 1995 Tinker Systems. |
|
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
5 ;; Copyright (C) 2002, 2010 Ben Wing. |
| 428 | 6 |
| 7 ;; Maintainer: FSF | |
| 8 ;; Keywords: lisp, languages, dumped | |
| 9 | |
| 10 ;; This file is part of XEmacs. | |
| 11 | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
12 ;; XEmacs is free software: you can redistribute it and/or modify it |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
13 ;; under the terms of the GNU General Public License as published by the |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
14 ;; Free Software Foundation, either version 3 of the License, or (at your |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
15 ;; option) any later version. |
| 428 | 16 |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
17 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
19 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
20 ;; for more details. |
| 428 | 21 |
| 22 ;; You should have received a copy of the GNU General Public License | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5194
diff
changeset
|
23 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
| 428 | 24 |
| 25 ;;; Synched up with: FSF 19.34 (but starting to diverge). | |
| 26 | |
| 27 ;;; Commentary: | |
| 28 | |
| 29 ;; This file is dumped with XEmacs. | |
| 30 | |
| 31 ;; The base major mode for editing Lisp code (used also for Emacs Lisp). | |
| 32 ;; This mode is documented in the Emacs manual | |
| 33 | |
| 34 ;;; Code: | |
| 35 | |
| 36 (defgroup lisp nil | |
| 37 "Lisp support, including Emacs Lisp." | |
| 38 :group 'languages | |
| 39 :group 'development) | |
| 40 | |
| 41 (defvar lisp-mode-syntax-table nil) | |
| 42 (defvar emacs-lisp-mode-syntax-table nil) | |
| 43 (defvar lisp-mode-abbrev-table nil) | |
| 44 | |
| 442 | 45 (defun construct-lisp-mode-menu (popup-p emacs-lisp-p) |
| 46 (flet ((popup-wrap (form) | |
| 47 (if popup-p `(menu-call-at-event ',form) form))) | |
| 48 `(,@(if emacs-lisp-p | |
| 49 `(["%_Byte-Compile This File" ,(popup-wrap | |
| 50 'emacs-lisp-byte-compile)] | |
| 51 ["B%_yte-Compile/Load This File" | |
| 52 ,(popup-wrap 'emacs-lisp-byte-compile-and-load)] | |
| 53 ["Byte-%_Recompile Directory..." | |
| 54 ,(popup-wrap 'byte-recompile-directory)] | |
| 55 "---")) | |
| 56 ["%_Evaluate Region or Defun" | |
| 57 ,(popup-wrap '(if (region-exists-p) | |
| 58 (call-interactively 'eval-region) | |
| 59 (call-interactively 'eval-defun)))] | |
| 60 ["Evaluate %_Whole Buffer" ,(popup-wrap 'eval-current-buffer)] | |
| 61 ["Evaluate Last %_S-expression" ,(popup-wrap 'eval-last-sexp)] | |
| 62 "---" | |
| 800 | 63 ["%_Indent Region or Balanced Expression" |
| 64 ,(popup-wrap '(if (region-exists-p) | |
| 65 (call-interactively 'indent-region) | |
| 66 (call-interactively 'indent-sexp)))] | |
| 67 ["I%_ndent Defun" | |
| 68 ,(popup-wrap '(progn | |
| 69 (beginning-of-defun) | |
| 70 (indent-sexp)))] | |
| 71 "---" | |
| 72 ["%_Comment Out Region" comment-region :active (region-exists-p)] | |
| 1333 | 73 ["Unc%_omment Region" uncomment-region :active (region-exists-p)] |
| 800 | 74 "---" |
| 442 | 75 ,@(if popup-p |
| 76 '(["%_Find Function" | |
| 77 (find-function (menu-call-at-event '(function-at-point))) | |
| 78 :suffix (let ((fun (menu-call-at-event '(function-at-point)))) | |
| 79 (if fun (symbol-name fun) "")) | |
| 80 :active (and (fboundp 'find-function) | |
| 81 (menu-call-at-event '(function-at-point)))] | |
| 82 ["%_Find Variable" | |
| 83 (find-variable (menu-call-at-event '(variable-at-point))) | |
| 84 :suffix (let ((fun (menu-call-at-event '(variable-at-point)))) | |
| 85 (if fun (symbol-name fun) "")) | |
| 86 :active (and (fboundp 'find-variable) | |
| 87 (menu-call-at-event '(variable-at-point)))] | |
| 88 ["%_Help on Function" | |
| 89 (describe-function (menu-call-at-event '(function-at-point))) | |
| 90 :suffix (let ((fun (menu-call-at-event '(function-at-point)))) | |
| 91 (if fun (symbol-name fun) "")) | |
| 92 :active (and (fboundp 'describe-function) | |
| 93 (menu-call-at-event '(function-at-point)))] | |
| 94 ["%_Help on Variable" | |
| 95 (describe-variable (menu-call-at-event '(variable-at-point))) | |
| 96 :suffix (let ((fun (menu-call-at-event '(variable-at-point)))) | |
| 97 (if fun (symbol-name fun) "")) | |
| 98 :active (and (fboundp 'describe-variable) | |
| 99 (menu-call-at-event '(variable-at-point)))]) | |
| 100 '(["Find %_Function..." find-function | |
| 101 :active (fboundp 'find-function)] | |
| 102 ["Find %_Variable..." find-variable | |
| 103 :active (fboundp 'find-variable)] | |
| 104 ["%_Help on Function..." describe-function | |
| 105 :active (fboundp 'describe-function)] | |
| 106 ["Hel%_p on Variable..." describe-variable | |
| 107 :active (fboundp 'describe-variable)])) | |
| 108 "---" | |
| 109 ["Instrument This Defun for %_Debugging" ,(popup-wrap 'edebug-defun)] | |
| 110 ["%_Trace Function..." trace-function-background] | |
| 111 ["%_Untrace All Functions" untrace-all | |
| 112 :active (fboundp 'untrace-all)] | |
| 113 "---" | |
| 800 | 114 ["Display %_Macro Expansion of Balanced Expression" |
| 115 ,(popup-wrap 'macroexpand-sexp)] | |
| 116 ["Display Recursive Macro E%_xpansion of Balanced Expression" | |
| 117 ,(popup-wrap 'macroexpand-all-sexp)] | |
| 442 | 118 "---" |
| 119 "Look for debug-on-error under Options->Troubleshooting" | |
| 120 ))) | |
| 428 | 121 |
| 442 | 122 (defvar lisp-interaction-mode-popup-menu |
| 123 (cons "Lisp-Interaction" (construct-lisp-mode-menu t nil))) | |
| 124 | |
| 125 (defvar emacs-lisp-mode-popup-menu | |
| 126 (cons "Emacs-Lisp" (construct-lisp-mode-menu t t))) | |
| 428 | 127 |
| 128 ;Don't have a menubar entry in Lisp Interaction mode. Otherwise, the | |
| 129 ;*scratch* buffer has a Lisp menubar item! Very confusing. | |
| 442 | 130 ;Jan Vroonhof really wants this, so it's back. --ben |
| 131 (defvar lisp-interaction-mode-menubar-menu | |
| 132 (cons "%_Lisp" (construct-lisp-mode-menu nil nil))) | |
| 428 | 133 |
| 442 | 134 (defvar emacs-lisp-mode-menubar-menu |
| 135 (cons "%_Lisp" (construct-lisp-mode-menu nil t))) | |
| 428 | 136 |
| 137 (if (not emacs-lisp-mode-syntax-table) | |
| 138 (let ((i 0)) | |
| 139 (setq emacs-lisp-mode-syntax-table (make-syntax-table)) | |
| 140 (while (< i ?0) | |
| 141 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
| 142 (setq i (1+ i))) | |
| 143 (setq i (1+ ?9)) | |
| 144 (while (< i ?A) | |
| 145 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
| 146 (setq i (1+ i))) | |
| 147 (setq i (1+ ?Z)) | |
| 148 (while (< i ?a) | |
| 149 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
| 150 (setq i (1+ i))) | |
| 151 (setq i (1+ ?z)) | |
| 152 (while (< i 128) | |
| 153 (modify-syntax-entry i "_ " emacs-lisp-mode-syntax-table) | |
| 154 (setq i (1+ i))) | |
| 155 (modify-syntax-entry ? " " emacs-lisp-mode-syntax-table) | |
| 156 (modify-syntax-entry ?\t " " emacs-lisp-mode-syntax-table) | |
| 157 (modify-syntax-entry ?\f " " emacs-lisp-mode-syntax-table) | |
| 158 (modify-syntax-entry ?\n "> " emacs-lisp-mode-syntax-table) | |
| 159 ;; Give CR the same syntax as newline, for selective-display. | |
| 160 (modify-syntax-entry ?\^m "> " emacs-lisp-mode-syntax-table) | |
| 161 (modify-syntax-entry ?\; "< " emacs-lisp-mode-syntax-table) | |
| 162 (modify-syntax-entry ?` "' " emacs-lisp-mode-syntax-table) | |
| 163 (modify-syntax-entry ?' "' " emacs-lisp-mode-syntax-table) | |
| 164 (modify-syntax-entry ?, "' " emacs-lisp-mode-syntax-table) | |
| 165 ;; Used to be singlequote; changed for flonums. | |
| 166 (modify-syntax-entry ?. "_ " emacs-lisp-mode-syntax-table) | |
| 167 (modify-syntax-entry ?# "' " emacs-lisp-mode-syntax-table) | |
| 168 (modify-syntax-entry ?\" "\" " emacs-lisp-mode-syntax-table) | |
| 169 (modify-syntax-entry ?\\ "\\ " emacs-lisp-mode-syntax-table) | |
| 170 (modify-syntax-entry ?\( "() " emacs-lisp-mode-syntax-table) | |
| 171 (modify-syntax-entry ?\) ")( " emacs-lisp-mode-syntax-table) | |
| 172 (modify-syntax-entry ?\[ "(] " emacs-lisp-mode-syntax-table) | |
| 173 (modify-syntax-entry ?\] ")[ " emacs-lisp-mode-syntax-table))) | |
| 174 | |
| 175 (if (not lisp-mode-syntax-table) | |
| 176 (progn (setq lisp-mode-syntax-table | |
| 177 (copy-syntax-table emacs-lisp-mode-syntax-table)) | |
| 178 (modify-syntax-entry ?\[ "_ " lisp-mode-syntax-table) | |
| 179 ;; XEmacs changes | |
| 180 (modify-syntax-entry ?\] "_ " lisp-mode-syntax-table) | |
| 460 | 181 (modify-syntax-entry ?# "' 58" lisp-mode-syntax-table) |
| 182 (modify-syntax-entry ?| "\" 67" lisp-mode-syntax-table))) | |
| 428 | 183 |
| 184 (define-abbrev-table 'lisp-mode-abbrev-table ()) | |
| 185 | |
| 186 (defvar lisp-imenu-generic-expression | |
| 187 '( | |
| 188 (nil | |
| 189 "^\\s-*(def\\(un\\|subst\\|macro\\|advice\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2) | |
| 190 ("Variables" | |
| 191 "^\\s-*(def\\(var\\|const\\|custom\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" 2) | |
| 192 ("Types" | |
| 193 "^\\s-*(def\\(group\\|type\\|struct\\|class\\|ine-condition\\)\\s-+\\([-A-Za-z0-9+*|:]+\\)" | |
| 194 2)) | |
| 195 | |
| 196 "Imenu generic expression for Lisp mode. See `imenu-generic-expression'.") | |
| 197 | |
| 198 (defun lisp-mode-variables (lisp-syntax) | |
| 199 (cond (lisp-syntax | |
| 200 (set-syntax-table lisp-mode-syntax-table))) | |
| 201 (setq local-abbrev-table lisp-mode-abbrev-table) | |
| 202 (make-local-variable 'paragraph-start) | |
| 203 (setq paragraph-start (concat page-delimiter "\\|$" )) | |
| 204 (make-local-variable 'paragraph-separate) | |
| 205 (setq paragraph-separate paragraph-start) | |
| 206 (make-local-variable 'paragraph-ignore-fill-prefix) | |
| 207 (setq paragraph-ignore-fill-prefix t) | |
| 208 (make-local-variable 'fill-paragraph-function) | |
| 209 (setq fill-paragraph-function 'lisp-fill-paragraph) | |
| 210 ;; Adaptive fill mode gets in the way of auto-fill, | |
| 211 ;; and should make no difference for explicit fill | |
| 212 ;; because lisp-fill-paragraph should do the job. | |
| 213 (make-local-variable 'adaptive-fill-mode) | |
| 214 (setq adaptive-fill-mode nil) | |
| 215 (make-local-variable 'indent-line-function) | |
| 216 (setq indent-line-function 'lisp-indent-line) | |
| 217 (make-local-variable 'indent-region-function) | |
| 218 (setq indent-region-function 'lisp-indent-region) | |
| 219 (make-local-variable 'parse-sexp-ignore-comments) | |
| 220 (setq parse-sexp-ignore-comments t) | |
| 221 (make-local-variable 'outline-regexp) | |
| 222 (setq outline-regexp ";;; \\|(....") | |
| 223 (make-local-variable 'comment-start) | |
| 224 (setq comment-start ";") | |
| 225 ;; XEmacs change | |
| 226 (set (make-local-variable 'block-comment-start) ";;") | |
| 227 (make-local-variable 'comment-start-skip) | |
| 228 ;; Look within the line for a ; following an even number of backslashes | |
| 229 ;; after either a non-backslash or the line beginning. | |
| 230 (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *") | |
| 231 (make-local-variable 'comment-column) | |
| 232 (setq comment-column 40) | |
| 233 (make-local-variable 'comment-indent-function) | |
| 234 (setq comment-indent-function 'lisp-comment-indent) | |
| 235 ;; XEmacs change | |
| 236 (set (make-local-variable 'dabbrev-case-fold-search) nil) | |
| 237 (set (make-local-variable 'dabbrev-case-replace) nil) | |
| 238 (make-local-variable 'imenu-generic-expression) | |
| 239 (setq imenu-generic-expression lisp-imenu-generic-expression)) | |
| 240 | |
| 241 (defvar shared-lisp-mode-map () | |
| 242 "Keymap for commands shared by all sorts of Lisp modes.") | |
| 243 | |
| 244 (if shared-lisp-mode-map | |
| 245 () | |
| 246 (setq shared-lisp-mode-map (make-sparse-keymap)) | |
| 247 ;; XEmacs changes | |
| 248 (set-keymap-name shared-lisp-mode-map 'shared-lisp-mode-map) | |
| 249 (define-key shared-lisp-mode-map "\M-;" 'lisp-indent-for-comment) | |
| 250 (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)) | |
| 251 | |
| 252 (defvar emacs-lisp-mode-map () | |
| 253 "Keymap for Emacs Lisp mode. | |
| 254 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
| 255 | |
| 256 (if emacs-lisp-mode-map | |
| 257 () | |
| 258 ;; XEmacs: Ignore FSF nconc stuff | |
| 259 (setq emacs-lisp-mode-map (make-sparse-keymap)) | |
| 260 (set-keymap-name emacs-lisp-mode-map 'emacs-lisp-mode-map) | |
| 261 (set-keymap-parents emacs-lisp-mode-map (list shared-lisp-mode-map)) | |
| 262 (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) | |
| 263 (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) | |
| 264 ;; XEmacs: Not sure what the FSF menu bindings are. I hope XEmacs | |
| 265 ;; doesn't need them. | |
| 266 ) | |
| 267 | |
| 1428 | 268 ;; XEmacs: add docstrings to the hooks |
| 269 (defvar emacs-lisp-mode-hook nil | |
| 270 "Hook to run when entering emacs-lisp-mode.") | |
| 271 | |
| 272 (defvar lisp-mode-hook nil | |
| 273 "Hook to run when entering lisp-mode.") | |
| 274 | |
| 275 (defvar lisp-interaction-mode-hook nil | |
| 276 "Hook to run when entering lisp-interaction-mode.") | |
| 277 | |
| 428 | 278 (defun emacs-lisp-byte-compile () |
| 279 "Byte compile the file containing the current buffer." | |
| 280 (interactive) | |
| 281 (if buffer-file-name | |
| 282 ;; XEmacs change. Force buffer save first | |
| 283 (progn | |
| 284 (save-buffer) | |
| 285 (byte-compile-file buffer-file-name)) | |
| 286 (error "The buffer must be saved in a file first."))) | |
| 287 | |
| 288 (defun emacs-lisp-byte-compile-and-load () | |
| 289 "Byte-compile the current file (if it has changed), then load compiled code." | |
| 290 (interactive) | |
| 291 (or buffer-file-name | |
| 292 (error "The buffer must be saved in a file first")) | |
| 293 (require 'bytecomp) | |
| 294 ;; Recompile if file or buffer has changed since last compilation. | |
| 295 (if (and (buffer-modified-p) | |
| 296 (y-or-n-p (format "save buffer %s first? " (buffer-name)))) | |
| 297 (save-buffer)) | |
| 298 (let ((compiled-file-name (byte-compile-dest-file buffer-file-name))) | |
| 299 (if (file-newer-than-file-p compiled-file-name buffer-file-name) | |
| 300 (load-file compiled-file-name) | |
| 301 (byte-compile-file buffer-file-name t)))) | |
| 302 | |
| 303 (defun emacs-lisp-mode () | |
| 304 "Major mode for editing Lisp code to run in Emacs. | |
| 305 Commands: | |
| 306 Delete converts tabs to spaces as it moves back. | |
| 307 Blank lines separate paragraphs. Semicolons start comments. | |
| 308 \\{emacs-lisp-mode-map} | |
| 309 Entry to this mode calls the value of `emacs-lisp-mode-hook' | |
| 310 if that value is non-nil." | |
| 311 (interactive) | |
| 312 (kill-all-local-variables) | |
| 313 (use-local-map emacs-lisp-mode-map) | |
| 314 (set-syntax-table emacs-lisp-mode-syntax-table) | |
| 315 ;; XEmacs changes | |
| 316 (setq major-mode 'emacs-lisp-mode | |
| 442 | 317 mode-popup-menu emacs-lisp-mode-popup-menu |
| 428 | 318 mode-name "Emacs-Lisp") |
| 442 | 319 (if (and (featurep 'menubar) |
| 320 current-menubar) | |
| 321 (progn | |
| 428 | 322 ;; make a local copy of the menubar, so our modes don't |
| 323 ;; change the global menubar | |
| 442 | 324 (set-buffer-menubar current-menubar) |
| 325 (add-submenu nil emacs-lisp-mode-menubar-menu))) | |
| 428 | 326 (lisp-mode-variables nil) |
| 327 (run-hooks 'emacs-lisp-mode-hook)) | |
| 328 | |
| 430 | 329 (put 'emacs-lisp-mode 'font-lock-lisp-like t) |
| 330 | |
| 428 | 331 (defvar lisp-mode-map () |
| 332 "Keymap for ordinary Lisp mode. | |
| 333 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
| 334 | |
| 335 (if lisp-mode-map | |
| 336 () | |
| 337 ;; XEmacs changes | |
| 338 (setq lisp-mode-map (make-sparse-keymap)) | |
| 339 (set-keymap-name lisp-mode-map 'lisp-mode-map) | |
| 340 (set-keymap-parents lisp-mode-map (list shared-lisp-mode-map)) | |
| 341 ;; gag, no. use ilisp. -jwz | |
| 342 ;; (define-key lisp-mode-map "\C-c\C-z" 'run-lisp) | |
| 343 ) | |
| 344 | |
| 345 (defun lisp-mode () | |
| 613 | 346 "Major mode for editing Lisp code for Lisps other than Emacs Lisp. |
| 428 | 347 Commands: |
| 348 Delete converts tabs to spaces as it moves back. | |
| 349 Blank lines separate paragraphs. Semicolons start comments. | |
| 350 \\{lisp-mode-map} | |
| 351 Note that `run-lisp' may be used either to start an inferior Lisp job | |
| 352 or to switch back to an existing one. | |
| 353 | |
| 354 Entry to this mode calls the value of `lisp-mode-hook' | |
| 355 if that value is non-nil." | |
| 356 (interactive) | |
| 357 (kill-all-local-variables) | |
| 358 (use-local-map lisp-mode-map) | |
| 359 (setq major-mode 'lisp-mode) | |
| 360 (setq mode-name "Lisp") | |
| 361 (lisp-mode-variables t) | |
| 362 (set-syntax-table lisp-mode-syntax-table) | |
| 363 (run-hooks 'lisp-mode-hook)) | |
| 364 | |
| 365 ;; XEmacs change: emacs-lisp-mode-map is a more appropriate parent. | |
| 366 (defvar lisp-interaction-mode-map () | |
| 367 "Keymap for Lisp Interaction mode. | |
| 368 All commands in `shared-lisp-mode-map' are inherited by this map.") | |
| 369 | |
| 370 (if lisp-interaction-mode-map | |
| 371 () | |
| 372 ;; XEmacs set keymap our way | |
| 373 (setq lisp-interaction-mode-map (make-sparse-keymap)) | |
| 374 (set-keymap-name lisp-interaction-mode-map 'lisp-interaction-mode-map) | |
| 375 (set-keymap-parents lisp-interaction-mode-map (list emacs-lisp-mode-map)) | |
| 376 (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun) | |
| 377 (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol) | |
| 378 (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp)) | |
| 379 | |
| 380 (defun lisp-interaction-mode () | |
| 381 "Major mode for typing and evaluating Lisp forms. | |
| 382 Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression | |
| 383 before point, and prints its value into the buffer, advancing point. | |
| 384 | |
| 385 Commands: | |
| 386 Delete converts tabs to spaces as it moves back. | |
| 387 Paragraphs are separated only by blank lines. | |
| 388 Semicolons start comments. | |
| 389 \\{lisp-interaction-mode-map} | |
| 390 Entry to this mode calls the value of `lisp-interaction-mode-hook' | |
| 391 if that value is non-nil." | |
| 392 (interactive) | |
| 393 (kill-all-local-variables) | |
| 394 (use-local-map lisp-interaction-mode-map) | |
| 395 (setq major-mode 'lisp-interaction-mode) | |
| 396 (setq mode-name "Lisp Interaction") | |
| 442 | 397 (setq mode-popup-menu lisp-interaction-mode-popup-menu) |
| 398 (if (and (featurep 'menubar) | |
| 399 current-menubar) | |
| 400 (progn | |
| 401 ;; make a local copy of the menubar, so our modes don't | |
| 402 ;; change the global menubar | |
| 403 (set-buffer-menubar current-menubar) | |
| 404 (add-submenu nil lisp-interaction-mode-menubar-menu))) | |
| 428 | 405 (set-syntax-table emacs-lisp-mode-syntax-table) |
| 406 (lisp-mode-variables nil) | |
| 407 (run-hooks 'lisp-interaction-mode-hook)) | |
| 408 | |
| 409 (defun eval-print-last-sexp () | |
| 410 "Evaluate sexp before point; print value into current buffer." | |
| 411 (interactive) | |
| 412 (let ((standard-output (current-buffer))) | |
| 413 (terpri) | |
| 414 (eval-last-sexp t) | |
| 415 (terpri))) | |
| 416 | |
| 417 ;; XEmacs change | |
| 418 (defcustom eval-interactive-verbose t | |
| 419 "*Non-nil means that interactive evaluation can print messages. | |
| 420 The messages are printed when the expression is treated differently | |
| 2116 | 421 using `\\[eval-last-sexp]' and `\\[eval-defun]' than it would have |
| 422 been treated noninteractively. | |
| 428 | 423 |
| 424 The printed messages are \"defvar treated as defconst\" and \"defcustom | |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
425 evaluation forced\". See `eval-interactive' for more details." |
| 428 | 426 :type 'boolean |
| 427 :group 'lisp) | |
| 428 | |
| 429 (defun eval-interactive (expr) | |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
430 "Evaluate EXPR; pass back multiple values, transform defvars to defconsts. |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
431 |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
432 Always returns a list. The length of this list will be something other than |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
433 one if the form returned multiple values. It will be zero if the form |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
434 returned a single zero-length multiple value." |
| 428 | 435 (cond ((and (eq (car-safe expr) 'defvar) |
| 436 (> (length expr) 2)) | |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
437 (setq expr (multiple-value-list (eval (cons 'defconst (cdr expr))))) |
| 428 | 438 (when eval-interactive-verbose |
| 439 (message "defvar treated as defconst") | |
| 440 (sit-for 1) | |
| 441 (message "")) | |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
442 expr) |
| 428 | 443 ((and (eq (car-safe expr) 'defcustom) |
| 444 (> (length expr) 2) | |
| 445 (default-boundp (nth 1 expr))) | |
| 446 ;; Force variable to be bound | |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
447 (funcall |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
448 (or (plist-get expr :set) #'custom-set-default) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
449 (nth 1 expr) (eval (nth 2 expr))) |
| 428 | 450 ;; And evaluate the defcustom |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
451 (setq expr (multiple-value-list (eval expr))) |
| 428 | 452 (when eval-interactive-verbose |
| 453 (message "defcustom evaluation forced") | |
| 454 (sit-for 1) | |
| 455 (message "")) | |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
456 expr) |
| 428 | 457 (t |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
458 (multiple-value-list (eval expr))))) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
459 |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
460 (defun prin1-list-as-multiple-values (multiple-value-list &optional stream) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
461 "Call `prin1' on each element of MULTIPLE-VALUE-LIST, separated by \" ;\\n\" |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
462 |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
463 If MULTIPLE-VALUE-LIST is zero-length, print the text |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
464 \"#<zero length multiple value> ;\\n\". Always returns nil." |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
465 (loop for value in multiple-value-list |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
466 with seen-first = nil |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
467 do |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
468 (if seen-first |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
469 (princ " ;\n" stream) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
470 (setq seen-first t)) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
471 (prin1 value stream) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
472 finally (unless seen-first |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
473 (princ "#<zero length multiple value> ;" stream)))) |
| 428 | 474 |
| 475 ;; XEmacs change, based on Bob Weiner suggestion | |
| 476 (defun eval-last-sexp (eval-last-sexp-arg-internal) ;dynamic scoping wonderment | |
| 477 "Evaluate sexp before point; print value in minibuffer. | |
| 478 With argument, print output into current buffer." | |
| 479 (interactive "P") | |
| 480 (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t)) | |
| 481 (opoint (point)) | |
| 482 ignore-quotes) | |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
483 (prin1-list-as-multiple-values |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
484 (eval-interactive |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
485 (letf (((syntax-table) emacs-lisp-mode-syntax-table)) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
486 (save-excursion |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
487 ;; If this sexp appears to be enclosed in `...' then |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
488 ;; ignore the surrounding quotes. |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
489 (setq ignore-quotes (or (eq (char-after) ?\') |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
490 (eq (char-before) ?\'))) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
491 (forward-sexp -1) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
492 ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
493 ;; `variable' so that the value is returned, not the |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
494 ;; name. |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
495 (if (and ignore-quotes |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
496 (eq (char-after) ?\`)) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
497 (forward-char)) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
498 (save-restriction |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
499 (narrow-to-region (point-min) opoint) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
500 (let ((expr (read (current-buffer)))) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
501 (if (eq (car-safe expr) 'interactive) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
502 ;; If it's an (interactive ...) form, it's |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
503 ;; more useful to show how an interactive call |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
504 ;; would use it. |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
505 `(call-interactively |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
506 (lambda (&rest args) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
507 ,expr args)) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
508 expr))))))))) |
| 428 | 509 |
| 510 (defun eval-defun (eval-defun-arg-internal) | |
| 511 "Evaluate defun that point is in or before. | |
| 512 Print value in minibuffer. | |
| 513 With argument, insert value in current buffer after the defun." | |
| 514 (interactive "P") | |
| 515 (let ((standard-output (if eval-defun-arg-internal (current-buffer) t))) | |
|
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
516 (prin1-list-as-multiple-values |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
517 (eval-interactive |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
518 (save-excursion |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
519 (end-of-defun) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
520 (beginning-of-defun) |
|
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3261
diff
changeset
|
521 (read (current-buffer))))))) |
| 428 | 522 |
| 523 (defun lisp-comment-indent () | |
| 524 (if (looking-at "\\s<\\s<\\s<") | |
| 525 (current-column) | |
| 526 (if (looking-at "\\s<\\s<") | |
| 527 ;; #### FSF has: | |
| 528 ;; (let ((tem (or (calculate-lisp-indent) (current-column)))) ... | |
| 529 (let ((tem (calculate-lisp-indent))) | |
| 530 (if (listp tem) (car tem) tem)) | |
| 531 (skip-chars-backward " \t") | |
| 532 (max (if (bolp) 0 (1+ (current-column))) | |
| 533 comment-column)))) | |
| 534 | |
| 535 ;; XEmacs change | |
| 536 (defun lisp-indent-for-comment () | |
| 537 "Indent this line's comment appropriately, or insert an empty comment. | |
| 538 If adding a new comment on a blank line, use `block-comment-start' instead | |
| 539 of `comment-start' to open the comment." | |
| 540 ;; by Stig@hackvan.com | |
| 541 ;; #### - This functionality, the recognition of block-comment-{start,end}, | |
| 542 ;; will perhaps be standardized across modes and move to indent-for-comment. | |
| 543 (interactive) | |
| 544 (if (and block-comment-start | |
| 545 (save-excursion (beginning-of-line) (looking-at "^[ \t]*$"))) | |
| 546 (insert block-comment-start)) | |
| 547 (indent-for-comment)) | |
| 548 | |
| 549 (defvar lisp-indent-offset nil) | |
| 550 (defvar lisp-indent-function 'lisp-indent-function) | |
| 551 | |
| 552 (defun lisp-indent-line (&optional whole-exp) | |
| 553 "Indent current line as Lisp code. | |
| 554 With argument, indent any additional lines of the same expression | |
| 555 rigidly along with this one." | |
| 556 (interactive "P") | |
| 557 (let ((indent (calculate-lisp-indent)) shift-amt beg end | |
| 558 (pos (- (point-max) (point)))) | |
| 559 (beginning-of-line) | |
| 560 (setq beg (point)) | |
| 561 (skip-chars-forward " \t") | |
| 562 (if (looking-at "\\s<\\s<\\s<") | |
| 563 ;; Don't alter indentation of a ;;; comment line. | |
| 564 (goto-char (- (point-max) pos)) | |
| 565 (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<"))) | |
| 566 ;; Single-semicolon comment lines should be indented | |
| 567 ;; as comment lines, not as code. | |
| 446 | 568 (progn (indent-for-comment) (backward-char 1)) |
| 428 | 569 (if (listp indent) (setq indent (car indent))) |
| 570 (setq shift-amt (- indent (current-column))) | |
| 571 (if (zerop shift-amt) | |
| 572 nil | |
| 573 (delete-region beg (point)) | |
| 574 (indent-to indent))) | |
| 575 ;; If initial point was within line's indentation, | |
| 576 ;; position after the indentation. Else stay at same point in text. | |
| 577 (if (> (- (point-max) pos) (point)) | |
| 578 (goto-char (- (point-max) pos))) | |
| 579 ;; If desired, shift remaining lines of expression the same amount. | |
| 580 (and whole-exp (not (zerop shift-amt)) | |
| 581 (save-excursion | |
| 582 (goto-char beg) | |
| 583 (forward-sexp 1) | |
| 584 (setq end (point)) | |
| 585 (goto-char beg) | |
| 586 (forward-line 1) | |
| 587 (setq beg (point)) | |
| 588 (> end beg)) | |
| 589 (indent-code-rigidly beg end shift-amt))))) | |
| 590 | |
| 591 (defvar calculate-lisp-indent-last-sexp) | |
| 592 | |
| 593 (defun calculate-lisp-indent (&optional parse-start) | |
| 594 "Return appropriate indentation for current line as Lisp code. | |
| 595 In usual case returns an integer: the column to indent to. | |
| 596 Can instead return a list, whose car is the column to indent to. | |
| 597 This means that following lines at the same level of indentation | |
| 598 should not necessarily be indented the same way. | |
| 599 The second element of the list is the buffer position | |
| 600 of the start of the containing expression." | |
| 601 (save-excursion | |
| 602 (beginning-of-line) | |
| 603 (let ((indent-point (point)) | |
| 604 ;; XEmacs change (remove paren-depth) | |
| 605 state ;;paren-depth | |
| 606 ;; setting this to a number inhibits calling hook | |
| 607 (desired-indent nil) | |
| 608 (retry t) | |
| 609 calculate-lisp-indent-last-sexp containing-sexp) | |
| 610 (if parse-start | |
| 611 (goto-char parse-start) | |
| 612 (beginning-of-defun)) | |
| 613 ;; Find outermost containing sexp | |
| 614 (while (< (point) indent-point) | |
| 615 (setq state (parse-partial-sexp (point) indent-point 0))) | |
| 616 ;; Find innermost containing sexp | |
| 617 (while (and retry | |
| 618 state | |
| 619 ;; XEmacs change (remove paren-depth) | |
| 620 (> ;;(setq paren-depth (elt state 0)) | |
| 621 (elt state 0) | |
| 622 0)) | |
| 623 (setq retry nil) | |
| 624 (setq calculate-lisp-indent-last-sexp (elt state 2)) | |
| 625 (setq containing-sexp (elt state 1)) | |
| 626 ;; Position following last unclosed open. | |
| 627 (goto-char (1+ containing-sexp)) | |
| 628 ;; Is there a complete sexp since then? | |
| 629 (if (and calculate-lisp-indent-last-sexp | |
| 630 (> calculate-lisp-indent-last-sexp (point))) | |
| 631 ;; Yes, but is there a containing sexp after that? | |
| 632 (let ((peek (parse-partial-sexp calculate-lisp-indent-last-sexp | |
| 633 indent-point 0))) | |
| 634 (if (setq retry (car (cdr peek))) (setq state peek))))) | |
| 635 (if retry | |
| 636 nil | |
| 637 ;; Innermost containing sexp found | |
| 638 (goto-char (1+ containing-sexp)) | |
| 639 (if (not calculate-lisp-indent-last-sexp) | |
| 640 ;; indent-point immediately follows open paren. | |
| 641 ;; Don't call hook. | |
| 642 (setq desired-indent (current-column)) | |
| 643 ;; Find the start of first element of containing sexp. | |
| 644 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
| 645 (cond ((looking-at "\\s(") | |
| 646 ;; First element of containing sexp is a list. | |
| 647 ;; Indent under that list. | |
| 648 ) | |
| 649 ((> (save-excursion (forward-line 1) (point)) | |
| 650 calculate-lisp-indent-last-sexp) | |
| 651 ;; This is the first line to start within the containing sexp. | |
| 652 ;; It's almost certainly a function call. | |
| 653 (if (= (point) calculate-lisp-indent-last-sexp) | |
| 654 ;; Containing sexp has nothing before this line | |
| 655 ;; except the first element. Indent under that element. | |
| 656 nil | |
| 657 ;; Skip the first element, find start of second (the first | |
| 658 ;; argument of the function call) and indent under. | |
| 659 (progn (forward-sexp 1) | |
| 660 (parse-partial-sexp (point) | |
| 661 calculate-lisp-indent-last-sexp | |
| 662 0 t))) | |
| 663 (backward-prefix-chars)) | |
| 664 (t | |
| 665 ;; Indent beneath first sexp on same line as | |
| 666 ;; calculate-lisp-indent-last-sexp. Again, it's | |
| 667 ;; almost certainly a function call. | |
| 668 (goto-char calculate-lisp-indent-last-sexp) | |
| 669 (beginning-of-line) | |
| 670 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp | |
| 671 0 t) | |
| 672 (backward-prefix-chars))))) | |
| 673 ;; Point is at the point to indent under unless we are inside a string. | |
| 674 ;; Call indentation hook except when overridden by lisp-indent-offset | |
| 675 ;; or if the desired indentation has already been computed. | |
| 676 (let ((normal-indent (current-column))) | |
| 677 (cond ((elt state 3) | |
| 678 ;; Inside a string, don't change indentation. | |
| 679 (goto-char indent-point) | |
| 680 (skip-chars-forward " \t") | |
| 681 (current-column)) | |
| 682 (desired-indent) | |
| 683 ((and (boundp 'lisp-indent-function) | |
| 684 lisp-indent-function | |
| 685 (not retry)) | |
| 686 (or (funcall lisp-indent-function indent-point state) | |
| 687 normal-indent)) | |
| 688 ;; XEmacs change: | |
| 689 ;; lisp-indent-offset shouldn't override lisp-indent-function ! | |
| 690 ((and (integerp lisp-indent-offset) containing-sexp) | |
| 691 ;; Indent by constant offset | |
| 692 (goto-char containing-sexp) | |
| 693 (+ normal-indent lisp-indent-offset)) | |
| 694 (t | |
| 695 normal-indent)))))) | |
| 696 | |
| 872 | 697 (defvar lisp-function-and-type-regexp |
| 698 (concat "def\\(" | |
| 699 ;; def but not define-. | |
|
5194
77907bd57d25
Add missing CL style fontification.
Didier Verna <didier@xemacs.org>
parents:
4958
diff
changeset
|
700 "\\(un\\*?\\|advice\\|alias\\|macro\\*?\\|setf\\|subst\\*?\\|" |
| 872 | 701 "-edebug-spec\\|" |
| 702 ;; CLOS | |
| 703 "method\\|generic\\|" | |
| 704 ;; define-* | |
| 705 "ine-\\(?:" | |
| 706 ;; basic Lisp stuff | |
| 707 "compiler-macro\\|function\\|function-when-void\\|modify-macro\\|" | |
| 708 "setf-method\\|" | |
| 709 ;; obsolete/compatible support, XEmacs-specific | |
| 710 "compatible-function-alias\\|obsolete-function-alias\\|" | |
| 711 ;; XEmacs-specific, supporting stuff inside of XEmacs | |
| 712 "ccl-program\\|device-method\\*?\\|prefix-command\\|skeleton" | |
| 713 "\\)\\)\\|" | |
| 714 ;; Structure declarations. | |
| 715 "\\(class\\|struct\\|type\\)\\)\\>") | |
| 716 "Regular expression to match the function and type keywords used in Lisp. | |
| 717 This matches, for example, the string \"defun\", as well as defsetf, | |
| 718 defsubst*, define-prefix-command, etc. Match string 1 matches everything | |
| 719 but the three-letter \"def\" string at the beginning. Match string 2 | |
| 720 matches everything after that, when it's *NOT* a \"type\" declaration -- | |
| 721 which includes defclass, defstruct, and deftype. Match string 3 is similar | |
| 722 to match string 2 in that it matches everything after the \"def\", when | |
| 723 \(and only when) the keyword matched *IS* a type declaration. You can use | |
| 724 match strings 2 and 3 to easily determine whether a function or type was | |
| 725 matched. The regex is terminated with a \\\> so that there must be a | |
| 726 word-end; i.e. defunbbb won't match.") | |
| 727 | |
| 728 (defvar lisp-flet-regexp | |
| 729 "(\\(flet\\|macrolet\\|labels\\)\\(\\s-\\|\n\\)") | |
| 730 | |
| 428 | 731 (defun lisp-indent-function (indent-point state) |
| 732 ;; free reference to `calculate-lisp-indent-last-sexp' | |
| 733 ;; in #'calculate-lisp-indent | |
| 734 (let ((normal-indent (current-column))) | |
| 735 (goto-char (1+ (elt state 1))) | |
| 736 (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) | |
| 737 (if (and (elt state 2) | |
| 738 (not (looking-at "\\sw\\|\\s_"))) | |
| 629 | 739 ;; car of form doesn't seem to be a symbol |
| 428 | 740 (progn |
| 741 (if (not (> (save-excursion (forward-line 1) (point)) | |
| 742 calculate-lisp-indent-last-sexp)) | |
| 743 (progn (goto-char calculate-lisp-indent-last-sexp) | |
| 744 (beginning-of-line) | |
| 745 (parse-partial-sexp (point) | |
| 746 calculate-lisp-indent-last-sexp 0 t))) | |
| 747 ;; Indent under the list or under the first sexp on the same | |
| 748 ;; line as calculate-lisp-indent-last-sexp. Note that first | |
| 749 ;; thing on that line has to be complete sexp since we are | |
| 750 ;; inside the innermost containing sexp. | |
| 751 (backward-prefix-chars) | |
| 752 (current-column)) | |
| 872 | 753 |
| 754 ;; Now come a bunch of ad-hoc checks to see if we're in particular | |
| 755 ;; places (defining an flet function, in the argument list of a | |
| 756 ;; regular or flet function, in a quoted list, etc.) where the | |
| 757 ;; regular indenting doesn't work well. | |
| 758 | |
| 759 ;; #### All this stuff here should be generalized so that | |
| 760 ;; you can specify, for various functions, how you want | |
| 761 ;; particular arguments handled -- in some way that works | |
| 762 ;; recursively, so it can handle flet and such. | |
| 763 | |
| 764 (let* ((function (buffer-substring (point) | |
| 428 | 765 (progn (forward-sexp 1) (point)))) |
| 872 | 766 (quote (condition-case nil |
| 767 (save-excursion | |
| 768 (backward-up-list 1) | |
| 769 (memq (char-before (point)) '(?' ?`))))) | |
| 770 method) | |
| 771 (cond | |
| 772 ;; if we're indenting a quoted list, and the first word is not | |
| 773 ;; lambda, line up second line below first with no indentation, | |
| 774 ;; so that property lists indent correctly. | |
| 775 ((and quote (not (equal function "lambda"))) | |
| 776 (setq method 'lisp-indent-quoteform)) | |
| 777 ;; do the same if we're indenting the arg list of a def* form. | |
| 778 ((let ((p (point))) | |
| 779 (condition-case nil | |
| 442 | 780 (save-excursion |
| 872 | 781 (backward-up-list 2) |
| 782 ;; make sure we're indeed the second argument of the | |
| 783 ;; def* form. | |
| 784 (and (eq (point) (save-excursion | |
| 785 (goto-char p) | |
| 786 (backward-up-list 1) | |
| 787 (backward-sexp 2) | |
| 788 (1- (point)))) | |
| 789 ;; check to see that the function is a def* type | |
| 790 (eq (char-after) ?\() | |
| 791 (progn (forward-char 1) t) | |
| 792 (looking-at lisp-function-and-type-regexp) | |
| 793 ;; defstruct may have slot option specs, which | |
| 794 ;; should probably be reverse-indented like | |
| 795 ;; normal, because the slot name is the first | |
| 796 ;; in the list. #### Review this. | |
| 797 (not (equal (match-string 0) "defstruct")))) | |
| 798 (error nil))) | |
| 799 (setq method 'lisp-indent-quoteform)) | |
| 800 | |
| 801 ;; handle functions in flet forms | |
| 802 ((let ((p (point))) | |
| 803 (condition-case nil | |
| 804 (save-excursion | |
| 805 (backward-up-list 3) | |
| 806 ;; make sure we're indeed a function, i.e. inside the | |
| 807 ;; first form after the flet. | |
| 808 (and (eq (point) (save-excursion | |
| 809 (goto-char p) | |
| 810 (backward-up-list 2) | |
| 811 (backward-sexp 1) | |
| 812 (1- (point)))) | |
| 813 (looking-at lisp-flet-regexp))) | |
| 814 (error nil))) | |
| 815 (setq method 'defun)) | |
| 816 | |
| 817 ;; handle the arg lists in functions in flet forms | |
| 818 ((let ((p (point))) | |
| 819 (condition-case nil | |
| 820 (save-excursion | |
| 821 (backward-up-list 2) | |
| 822 ;; make sure we're indeed the arg list -- i.e. the first | |
| 823 ;; element after the function name. | |
| 824 (and (eq (point) (save-excursion | |
| 825 (goto-char p) | |
| 826 (backward-up-list 1) | |
| 827 (backward-sexp 1) | |
| 828 (1- (point)))) | |
| 829 (progn | |
| 830 (backward-up-list 2) | |
| 831 (looking-at lisp-flet-regexp)))) | |
| 832 (error nil))) | |
| 833 (setq method 'lisp-indent-quoteform)) | |
| 834 (t | |
| 835 (setq method | |
| 836 (or (get (intern-soft function) 'lisp-indent-function) | |
| 837 (get (intern-soft function) 'lisp-indent-hook))))) | |
| 428 | 838 (cond ((or (eq method 'defun) |
| 839 (and (null method) | |
| 840 (> (length function) 3) | |
| 841 (string-match "\\`def" function))) | |
| 842 (lisp-indent-defform state indent-point)) | |
| 843 ((integerp method) | |
| 844 (lisp-indent-specform method state | |
| 845 indent-point normal-indent)) | |
| 846 (method | |
|
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
847 (funcall method state indent-point)) |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
848 ((and (> (length function) 5) |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
849 (string-match "\\`with-" function)) |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
850 (lisp-indent-specform 0 state indent-point normal-indent |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
851 t))))))) |
| 428 | 852 |
| 872 | 853 (defun lisp-indent-quoteform (state indent-point) |
| 854 (goto-char (car (cdr state))) | |
| 855 (forward-line 1) | |
| 856 (if (> (point) (car (cdr (cdr state)))) | |
| 857 (progn | |
| 858 (goto-char (car (cdr state))) | |
| 859 (+ 1 (current-column))))) | |
| 860 | |
| 428 | 861 (defvar lisp-body-indent 2 |
| 862 "Number of columns to indent the second line of a `(def...)' form.") | |
| 863 | |
|
4958
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
864 ;; Calculate the appropriate indentation for a special form (e.g. defun) or |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
865 ;; a macro that behaves like a special form (e.g. with-temp-buffer). |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
866 ;; Return value is as for `calculate-lisp-indent' (q.v.). |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
867 ;; |
|
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
868 ;; COUNT is the number of "distinguished" forms (e.g. arguments before the |
|
4958
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
869 ;; "body", in a macro taking a body as its last argument). |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
870 ;; |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
871 ;; INDENT-POINT is usually the value of (point) at the beginning of the |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
872 ;; line containing the form to be indented. |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
873 ;; |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
874 ;; STATE is the result of calling (parse-partial-sexp (point) INDENT-POINT) |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
875 ;; when (point) is sitting on (i.e. just before) the outermost containing |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
876 ;; left paren for the form to be indented. |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
877 ;; |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
878 ;; NORMAL-INDENT is the amount of "full" indentation used for arguments |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
879 ;; that aren't given some special indentation (usually less), and normally |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
880 ;; it lines up with the first word after the function name. forms are |
|
e4bbe5622a80
finish doc comment on `lisp-indent-specform'
Ben Wing <ben@xemacs.org>
parents:
4942
diff
changeset
|
881 ;; indented as follows: |
|
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
882 ;; |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
883 ;; -- The first and second distinguished forms are given 2 times body indent |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
884 ;; (determined by `lisp-body-indent') |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
885 ;; -- Other distinguished forms are given normal indent |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
886 ;; -- The first nondistinguished form (the body, usually) is given body indent |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
887 ;; -- Normally, any further nondistinguished forms are given normal indent |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
888 ;; -- However, if INDENT-REMAINING-AS-BODY was specified, any further |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
889 ;; nondistinguished forms are given body indent. This is useful when the |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
890 ;; number of distinguished forms isn't known or may vary. |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
891 |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
892 (defun lisp-indent-specform (count state indent-point normal-indent |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
893 &optional indent-remaining-as-body) |
| 428 | 894 (let ((containing-form-start (elt state 1)) |
| 895 (i count) | |
| 896 body-indent containing-form-column) | |
| 897 ;; Move to the start of containing form, calculate indentation | |
| 898 ;; to use for non-distinguished forms (> count), and move past the | |
| 899 ;; function symbol. lisp-indent-function guarantees that there is at | |
| 900 ;; least one word or symbol character following open paren of containing | |
| 901 ;; form. | |
| 902 (goto-char containing-form-start) | |
| 903 (setq containing-form-column (current-column)) | |
| 904 (setq body-indent (+ lisp-body-indent containing-form-column)) | |
| 905 (forward-char 1) | |
| 906 (forward-sexp 1) | |
| 907 ;; Now find the start of the last form. | |
| 908 (parse-partial-sexp (point) indent-point 1 t) | |
| 909 (while (and (< (point) indent-point) | |
| 910 (condition-case () | |
| 911 (progn | |
| 912 (setq count (1- count)) | |
| 913 (forward-sexp 1) | |
| 914 (parse-partial-sexp (point) indent-point 1 t)) | |
| 915 (error nil)))) | |
| 916 ;; Point is sitting on first character of last (or count) sexp. | |
| 917 (if (> count 0) | |
| 918 ;; A distinguished form. If it is the first or second form use double | |
| 919 ;; lisp-body-indent, else normal indent. With lisp-body-indent bound | |
| 920 ;; to 2 (the default), this just happens to work the same with if as | |
| 921 ;; the older code, but it makes unwind-protect, condition-case, | |
| 922 ;; with-output-to-temp-buffer, et. al. much more tasteful. The older, | |
| 923 ;; less hacked, behavior can be obtained by replacing below with | |
| 924 ;; (list normal-indent containing-form-start). | |
| 925 (if (<= (- i count) 1) | |
| 926 (list (+ containing-form-column (* 2 lisp-body-indent)) | |
| 927 containing-form-start) | |
| 928 (list normal-indent containing-form-start)) | |
| 929 ;; A non-distinguished form. Use body-indent if there are no | |
| 930 ;; distinguished forms and this is the first undistinguished form, | |
| 931 ;; or if this is the first undistinguished form and the preceding | |
| 932 ;; distinguished form has indentation at least as great as body-indent. | |
| 933 (if (or (and (= i 0) (= count 0)) | |
|
4942
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
934 (and (or (= count 0) indent-remaining-as-body) |
|
46f0df723e09
(for main branch) Indent all `with-*' expressions correctly
Ben Wing <ben@xemacs.org>
parents:
4677
diff
changeset
|
935 (<= body-indent normal-indent))) |
| 428 | 936 body-indent |
| 937 normal-indent)))) | |
| 938 | |
| 939 (defun lisp-indent-defform (state indent-point) | |
| 940 (goto-char (car (cdr state))) | |
| 941 (forward-line 1) | |
| 942 (if (> (point) (car (cdr (cdr state)))) | |
| 943 (progn | |
| 944 (goto-char (car (cdr state))) | |
| 945 (+ lisp-body-indent (current-column))))) | |
| 946 | |
| 947 | |
| 948 ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented | |
| 949 ;; like defun if the first form is placed on the next line, otherwise | |
| 950 ;; it is indented like any other form (i.e. forms line up under first). | |
| 951 | |
| 776 | 952 ;; NOTE: All def* functions are handled specially, no need to put them here. |
| 953 ;; NOTE: All cl-* constructs are handled in cl.el. | |
| 954 | |
| 428 | 955 (put 'lambda 'lisp-indent-function 'defun) |
| 956 (put 'autoload 'lisp-indent-function 'defun) | |
| 957 (put 'progn 'lisp-indent-function 0) | |
| 958 (put 'prog1 'lisp-indent-function 1) | |
| 959 (put 'prog2 'lisp-indent-function 2) | |
| 960 (put 'save-excursion 'lisp-indent-function 0) | |
| 961 (put 'save-window-excursion 'lisp-indent-function 0) | |
| 962 (put 'save-selected-window 'lisp-indent-function 0) | |
| 442 | 963 (put 'with-selected-window 'lisp-indent-function 1) |
| 428 | 964 (put 'save-selected-frame 'lisp-indent-function 0) |
| 965 (put 'with-selected-frame 'lisp-indent-function 1) | |
| 872 | 966 (put 'save-selected-device 'lisp-indent-function 0) |
| 967 (put 'with-selected-device 'lisp-indent-function 1) | |
| 428 | 968 (put 'save-restriction 'lisp-indent-function 0) |
| 969 (put 'save-match-data 'lisp-indent-function 0) | |
| 970 (put 'let 'lisp-indent-function 1) | |
| 971 (put 'let* 'lisp-indent-function 1) | |
| 972 (put 'let-specifier 'lisp-indent-function 1) | |
| 973 (put 'while 'lisp-indent-function 1) | |
| 974 (put 'if 'lisp-indent-function 2) | |
| 975 (put 'catch 'lisp-indent-function 1) | |
| 976 (put 'condition-case 'lisp-indent-function 2) | |
| 460 | 977 (put 'handler-case 'lisp-indent-function 1) |
| 978 (put 'handler-bind 'lisp-indent-function 1) | |
| 428 | 979 (put 'call-with-condition-handler 'lisp-indent-function 2) |
| 980 (put 'unwind-protect 'lisp-indent-function 1) | |
| 981 (put 'save-current-buffer 'lisp-indent-function 0) | |
| 982 (put 'with-current-buffer 'lisp-indent-function 1) | |
| 983 (put 'with-string-as-buffer-contents 'lisp-indent-function 1) | |
| 984 (put 'with-temp-file 'lisp-indent-function 1) | |
| 985 (put 'with-temp-buffer 'lisp-indent-function 0) | |
| 986 (put 'with-output-to-string 'lisp-indent-function 0) | |
| 987 (put 'with-output-to-temp-buffer 'lisp-indent-function 1) | |
| 460 | 988 (put 'with-slots 'lisp-indent-function 2) |
| 989 (put 'with-open-file 'lisp-indent-function 1) | |
| 990 (put 'with-open-stream 'lisp-indent-function 1) | |
| 428 | 991 (put 'eval-after-load 'lisp-indent-function 1) |
| 992 (put 'display-message 'lisp-indent-function 1) | |
| 993 (put 'display-warning 'lisp-indent-function 1) | |
| 994 (put 'lmessage 'lisp-indent-function 2) | |
| 995 (put 'lwarn 'lisp-indent-function 2) | |
| 996 (put 'global-set-key 'lisp-indent-function 1) | |
| 460 | 997 (put 'print-unreadable-object 'lisp-indent-function 1) |
| 428 | 998 |
| 999 (defun indent-sexp (&optional endpos) | |
| 1000 "Indent each line of the list starting just after point. | |
| 1001 If optional arg ENDPOS is given, indent each line, stopping when | |
| 1002 ENDPOS is encountered." | |
| 1003 (interactive) | |
| 1004 (let ((indent-stack (list nil)) | |
| 1005 (next-depth 0) | |
| 1006 ;; If ENDPOS is non-nil, use nil as STARTING-POINT | |
| 1007 ;; so that calculate-lisp-indent will find the beginning of | |
| 1008 ;; the defun we are in. | |
| 1009 ;; If ENDPOS is nil, it is safe not to scan before point | |
| 1010 ;; since every line we indent is more deeply nested than point is. | |
| 1011 (starting-point (if endpos nil (point))) | |
| 1012 (last-point (point)) | |
| 1013 last-depth bol outer-loop-done inner-loop-done state this-indent) | |
| 1014 (or endpos | |
| 1015 ;; Get error now if we don't have a complete sexp after point. | |
| 1016 (save-excursion (forward-sexp 1))) | |
| 1017 (save-excursion | |
| 1018 (setq outer-loop-done nil) | |
| 1019 (while (if endpos (< (point) endpos) | |
| 1020 (not outer-loop-done)) | |
| 1021 (setq last-depth next-depth | |
| 1022 inner-loop-done nil) | |
| 1023 ;; Parse this line so we can learn the state | |
| 1024 ;; to indent the next line. | |
| 1025 ;; This inner loop goes through only once | |
| 1026 ;; unless a line ends inside a string. | |
| 1027 (while (and (not inner-loop-done) | |
| 1028 (not (setq outer-loop-done (eobp)))) | |
| 1029 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
| 1030 nil nil state)) | |
| 1031 (setq next-depth (car state)) | |
| 1032 ;; If the line contains a comment other than the sort | |
| 1033 ;; that is indented like code, | |
| 1034 ;; indent it now with indent-for-comment. | |
| 1035 ;; Comments indented like code are right already. | |
| 1036 ;; In any case clear the in-comment flag in the state | |
| 1037 ;; because parse-partial-sexp never sees the newlines. | |
| 1038 (if (car (nthcdr 4 state)) | |
| 1039 (progn (indent-for-comment) | |
| 1040 (end-of-line) | |
| 1041 (setcar (nthcdr 4 state) nil))) | |
| 1042 ;; If this line ends inside a string, | |
| 1043 ;; go straight to next line, remaining within the inner loop, | |
| 1044 ;; and turn off the \-flag. | |
| 1045 (if (car (nthcdr 3 state)) | |
| 1046 (progn | |
| 1047 (forward-line 1) | |
| 1048 (setcar (nthcdr 5 state) nil)) | |
| 1049 (setq inner-loop-done t))) | |
| 1050 (and endpos | |
| 1051 (<= next-depth 0) | |
| 1052 (progn | |
| 1053 (setq indent-stack (append indent-stack | |
| 1054 (make-list (- next-depth) nil)) | |
| 1055 last-depth (- last-depth next-depth) | |
| 1056 next-depth 0))) | |
| 1057 (or outer-loop-done endpos | |
| 1058 (setq outer-loop-done (<= next-depth 0))) | |
| 1059 (if outer-loop-done | |
| 1060 (forward-line 1) | |
| 1061 (while (> last-depth next-depth) | |
| 1062 (setq indent-stack (cdr indent-stack) | |
| 1063 last-depth (1- last-depth))) | |
| 1064 (while (< last-depth next-depth) | |
| 1065 (setq indent-stack (cons nil indent-stack) | |
| 1066 last-depth (1+ last-depth))) | |
| 1067 ;; Now go to the next line and indent it according | |
| 1068 ;; to what we learned from parsing the previous one. | |
| 1069 (forward-line 1) | |
| 1070 (setq bol (point)) | |
| 1071 (skip-chars-forward " \t") | |
| 1072 ;; But not if the line is blank, or just a comment | |
| 1073 ;; (except for double-semi comments; indent them as usual). | |
| 1074 (if (or (eobp) (looking-at "\\s<\\|\n")) | |
| 1075 nil | |
| 1076 (if (and (car indent-stack) | |
| 1077 (>= (car indent-stack) 0)) | |
| 1078 (setq this-indent (car indent-stack)) | |
| 1079 (let ((val (calculate-lisp-indent | |
| 1080 (if (car indent-stack) (- (car indent-stack)) | |
| 1081 starting-point)))) | |
| 1082 (if (integerp val) | |
| 1083 (setcar indent-stack | |
| 1084 (setq this-indent val)) | |
| 1085 (setcar indent-stack (- (car (cdr val)))) | |
| 1086 (setq this-indent (car val))))) | |
| 1087 (if (/= (current-column) this-indent) | |
| 1088 (progn (delete-region bol (point)) | |
| 1089 (indent-to this-indent))))) | |
| 1090 (or outer-loop-done | |
| 1091 (setq outer-loop-done (= (point) last-point)) | |
| 1092 (setq last-point (point))))))) | |
| 1093 | |
| 1094 ;; Indent every line whose first char is between START and END inclusive. | |
| 1095 (defun lisp-indent-region (start end) | |
| 1096 (save-excursion | |
| 1097 (let ((endmark (copy-marker end))) | |
| 1098 (goto-char start) | |
| 1099 (and (bolp) (not (eolp)) | |
| 1100 (lisp-indent-line)) | |
| 1101 (indent-sexp endmark) | |
| 1102 (set-marker endmark nil)))) | |
| 1103 | |
| 1104 ;;;; Lisp paragraph filling commands. | |
| 1105 | |
| 1106 (defun lisp-fill-paragraph (&optional justify) | |
| 1107 "Like \\[fill-paragraph], but handle Emacs Lisp comments. | |
| 1108 If any of the current line is a comment, fill the comment or the | |
| 1109 paragraph of it that point is in, preserving the comment's indentation | |
| 1110 and initial semicolons." | |
| 1111 (interactive "P") | |
| 1112 (let ( | |
| 1113 ;; Non-nil if the current line contains a comment. | |
| 1114 has-comment | |
| 1115 | |
| 1116 ;; Non-nil if the current line contains code and a comment. | |
| 1117 has-code-and-comment | |
| 1118 | |
| 1119 ;; If has-comment, the appropriate fill-prefix for the comment. | |
| 1120 comment-fill-prefix | |
| 1121 ) | |
| 1122 | |
| 1123 ;; Figure out what kind of comment we are looking at. | |
| 1124 (save-excursion | |
| 1125 (beginning-of-line) | |
| 1126 (cond | |
| 1127 | |
| 1128 ;; A line with nothing but a comment on it? | |
| 1129 ((looking-at "[ \t]*;[; \t]*") | |
| 1130 (setq has-comment t | |
| 1131 comment-fill-prefix (buffer-substring (match-beginning 0) | |
| 1132 (match-end 0)))) | |
| 1133 | |
| 1134 ;; A line with some code, followed by a comment? Remember that the | |
| 1135 ;; semi which starts the comment shouldn't be part of a string or | |
| 1136 ;; character. | |
| 1137 ;; XEmacs Try this the FSF and see if it works. | |
| 1138 ; ((progn | |
| 1139 ; (while (not (looking-at ";\\|$")) | |
| 1140 ; (skip-chars-forward "^;\n\"\\\\?") | |
| 1141 ; (cond | |
| 1142 ; ((eq (char-after (point)) ?\\) (forward-char 2)) | |
| 1143 ; ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
| 1144 ; (looking-at ";+[\t ]*")) | |
| 1145 ; (setq has-comment t) | |
| 1146 ((condition-case nil | |
| 1147 (save-restriction | |
| 1148 (narrow-to-region (point-min) | |
| 1149 (save-excursion (end-of-line) (point))) | |
| 1150 (while (not (looking-at ";\\|$")) | |
| 1151 (skip-chars-forward "^;\n\"\\\\?") | |
| 1152 (cond | |
| 1153 ((eq (char-after (point)) ?\\) (forward-char 2)) | |
| 1154 ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1)))) | |
| 844 | 1155 ;; don't do comment filling in a string, or we will mess up |
| 1156 ;; doc strings and other places where semicolons are used. | |
| 1157 (and (not (eq 'string (buffer-syntactic-context))) | |
| 1158 (looking-at ";+[\t ]*"))) | |
| 428 | 1159 (error nil)) |
| 1160 (setq has-comment t has-code-and-comment t) | |
| 1161 (setq comment-fill-prefix | |
| 1162 (concat (make-string (/ (current-column) 8) ?\t) | |
| 1163 (make-string (% (current-column) 8) ?\ ) | |
| 1164 (buffer-substring (match-beginning 0) (match-end 0))))))) | |
| 1165 | |
| 1166 (if (not has-comment) | |
| 1167 (fill-paragraph justify) | |
| 1168 | |
| 1169 ;; Narrow to include only the comment, and then fill the region. | |
| 1170 (save-excursion | |
| 1171 (save-restriction | |
| 1172 (beginning-of-line) | |
| 1173 (narrow-to-region | |
| 1174 ;; Find the first line we should include in the region to fill. | |
| 1175 (save-excursion | |
| 1176 (while (and (zerop (forward-line -1)) | |
| 1177 (looking-at "^[ \t]*;"))) | |
| 1178 ;; We may have gone too far. Go forward again. | |
| 1179 (or (looking-at ".*;") | |
| 1180 (forward-line 1)) | |
| 1181 (point)) | |
| 1182 ;; Find the beginning of the first line past the region to fill. | |
| 1183 (save-excursion | |
| 1184 (while (progn (forward-line 1) | |
| 1185 (looking-at "^[ \t]*;"))) | |
| 1186 (point))) | |
| 1187 | |
| 1188 ;; Lines with only semicolons on them can be paragraph boundaries. | |
| 1189 (let* ((paragraph-start (concat paragraph-start "\\|[ \t;]*$")) | |
| 1190 (paragraph-separate (concat paragraph-start "\\|[ \t;]*$")) | |
| 1191 (paragraph-ignore-fill-prefix nil) | |
| 1192 (fill-prefix comment-fill-prefix) | |
| 1193 (after-line (if has-code-and-comment | |
| 1194 (save-excursion | |
| 1195 (forward-line 1) (point)))) | |
| 1196 (end (progn | |
| 1197 (forward-paragraph) | |
| 1198 (or (bolp) (newline 1)) | |
| 1199 (point))) | |
| 1200 ;; If this comment starts on a line with code, | |
| 1201 ;; include that like in the filling. | |
| 1202 (beg (progn (backward-paragraph) | |
| 1203 (if (eq (point) after-line) | |
| 1204 (forward-line -1)) | |
| 1205 (point)))) | |
| 1206 (fill-region-as-paragraph beg end | |
| 1207 justify nil | |
| 1208 (save-excursion | |
| 1209 (goto-char beg) | |
| 1210 (if (looking-at fill-prefix) | |
| 1211 nil | |
| 1212 (re-search-forward comment-start-skip) | |
| 1213 (point)))))))) | |
| 1214 t)) | |
| 1215 | |
| 1216 (defun indent-code-rigidly (start end arg &optional nochange-regexp) | |
| 1217 "Indent all lines of code, starting in the region, sideways by ARG columns. | |
| 1218 Does not affect lines starting inside comments or strings, assuming that | |
| 1219 the start of the region is not inside them. | |
| 1220 | |
| 1221 Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP. | |
| 1222 The last is a regexp which, if matched at the beginning of a line, | |
| 1223 means don't indent that line." | |
| 1224 (interactive "r\np") | |
| 1225 (let (state) | |
| 1226 (save-excursion | |
| 1227 (goto-char end) | |
| 1228 (setq end (point-marker)) | |
| 1229 (goto-char start) | |
| 1230 (or (bolp) | |
| 1231 (setq state (parse-partial-sexp (point) | |
| 1232 (progn | |
| 1233 (forward-line 1) (point)) | |
| 1234 nil nil state))) | |
| 1235 (while (< (point) end) | |
| 1236 (or (car (nthcdr 3 state)) | |
| 1237 (and nochange-regexp | |
| 1238 (looking-at nochange-regexp)) | |
| 1239 ;; If line does not start in string, indent it | |
| 1240 (let ((indent (current-indentation))) | |
| 1241 (delete-region (point) (progn (skip-chars-forward " \t") (point))) | |
| 1242 (or (eolp) | |
| 1243 (indent-to (max 0 (+ indent arg)) 0)))) | |
| 1244 (setq state (parse-partial-sexp (point) | |
| 1245 (progn | |
| 1246 (forward-line 1) (point)) | |
| 1247 nil nil state)))))) | |
| 1248 | |
| 1249 (provide 'lisp-mode) | |
| 1250 | |
| 1251 ;;; lisp-mode.el ends here |
