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