X-Git-Url: http://git.hcoop.net/bpt/emacs.git/blobdiff_plain/49d395cd57e646162e7f646a8561a416abacac82..5396468298b0122469e0b41da8f49860d99a2b51:/lisp/emacs-lisp/lisp-mode.el diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index d5588f3811..f9e7fe4482 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -1,17 +1,17 @@ ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands -;; Copyright (C) 1985, 1986, 1999, 2000, 2001, 2002, 2003, 2004, -;; 2005, 2006 Free Software Foundation, Inc. +;; Copyright (C) 1985-1986, 1999-2011 Free Software Foundation, Inc. ;; Maintainer: FSF ;; Keywords: lisp, languages +;; Package: emacs ;; This file is part of GNU Emacs. -;; GNU Emacs is free software; you can redistribute it and/or modify +;; GNU Emacs is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by -;; the Free Software Foundation; either version 2, or (at your option) -;; any later version. +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,9 +19,7 @@ ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License -;; along with GNU Emacs; see the file COPYING. If not, write to the -;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, -;; Boston, MA 02110-1301, USA. +;; along with GNU Emacs. If not, see . ;;; Commentary: @@ -37,56 +35,60 @@ (defvar lisp-mode-abbrev-table nil) +(define-abbrev-table 'lisp-mode-abbrev-table ()) + (defvar emacs-lisp-mode-syntax-table - (let ((table (make-syntax-table))) - (let ((i 0)) - (while (< i ?0) - (modify-syntax-entry i "_ " table) - (setq i (1+ i))) - (setq i (1+ ?9)) - (while (< i ?A) - (modify-syntax-entry i "_ " table) - (setq i (1+ i))) - (setq i (1+ ?Z)) - (while (< i ?a) - (modify-syntax-entry i "_ " table) - (setq i (1+ i))) - (setq i (1+ ?z)) - (while (< i 128) - (modify-syntax-entry i "_ " table) - (setq i (1+ i))) - (modify-syntax-entry ?\s " " table) - (modify-syntax-entry ?\t " " table) - (modify-syntax-entry ?\f " " table) - (modify-syntax-entry ?\n "> " table) - ;; This is probably obsolete since nowadays such features use overlays. - ;; ;; Give CR the same syntax as newline, for selective-display. - ;; (modify-syntax-entry ?\^m "> " table) - (modify-syntax-entry ?\; "< " table) - (modify-syntax-entry ?` "' " table) - (modify-syntax-entry ?' "' " table) - (modify-syntax-entry ?, "' " table) - (modify-syntax-entry ?@ "' " table) - ;; Used to be singlequote; changed for flonums. - (modify-syntax-entry ?. "_ " table) - (modify-syntax-entry ?# "' " table) - (modify-syntax-entry ?\" "\" " table) - (modify-syntax-entry ?\\ "\\ " table) - (modify-syntax-entry ?\( "() " table) - (modify-syntax-entry ?\) ")( " table) - (modify-syntax-entry ?\[ "(] " table) - (modify-syntax-entry ?\] ")[ " table)) - table)) + (let ((table (make-syntax-table)) + (i 0)) + (while (< i ?0) + (modify-syntax-entry i "_ " table) + (setq i (1+ i))) + (setq i (1+ ?9)) + (while (< i ?A) + (modify-syntax-entry i "_ " table) + (setq i (1+ i))) + (setq i (1+ ?Z)) + (while (< i ?a) + (modify-syntax-entry i "_ " table) + (setq i (1+ i))) + (setq i (1+ ?z)) + (while (< i 128) + (modify-syntax-entry i "_ " table) + (setq i (1+ i))) + (modify-syntax-entry ?\s " " table) + ;; Non-break space acts as whitespace. + (modify-syntax-entry ?\x8a0 " " table) + (modify-syntax-entry ?\t " " table) + (modify-syntax-entry ?\f " " table) + (modify-syntax-entry ?\n "> " table) + ;; This is probably obsolete since nowadays such features use overlays. + ;; ;; Give CR the same syntax as newline, for selective-display. + ;; (modify-syntax-entry ?\^m "> " table) + (modify-syntax-entry ?\; "< " table) + (modify-syntax-entry ?` "' " table) + (modify-syntax-entry ?' "' " table) + (modify-syntax-entry ?, "' " table) + (modify-syntax-entry ?@ "' " table) + ;; Used to be singlequote; changed for flonums. + (modify-syntax-entry ?. "_ " table) + (modify-syntax-entry ?# "' " table) + (modify-syntax-entry ?\" "\" " table) + (modify-syntax-entry ?\\ "\\ " table) + (modify-syntax-entry ?\( "() " table) + (modify-syntax-entry ?\) ")( " table) + (modify-syntax-entry ?\[ "(] " table) + (modify-syntax-entry ?\] ")[ " table) + table) + "Syntax table used in `emacs-lisp-mode'.") (defvar lisp-mode-syntax-table (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table))) (modify-syntax-entry ?\[ "_ " table) (modify-syntax-entry ?\] "_ " table) - (modify-syntax-entry ?# "' 14b" table) + (modify-syntax-entry ?# "' 14" table) (modify-syntax-entry ?| "\" 23bn" table) - table)) - -(define-abbrev-table 'lisp-mode-abbrev-table ()) + table) + "Syntax table used in `lisp-mode'.") (defvar lisp-imenu-generic-expression (list @@ -97,6 +99,7 @@ '("defun" "defun*" "defsubst" "defmacro" "defadvice" "define-skeleton" "define-minor-mode" "define-global-minor-mode" + "define-globalized-minor-mode" "define-derived-mode" "define-generic-mode" "define-compiler-macro" "define-modify-macro" "defsetf" "define-setf-expander" @@ -131,6 +134,7 @@ (put 'defvar 'doc-string-elt 3) (put 'defcustom 'doc-string-elt 3) (put 'deftheme 'doc-string-elt 2) +(put 'deftype 'doc-string-elt 3) (put 'defconst 'doc-string-elt 3) (put 'defmacro 'doc-string-elt 3) (put 'defmacro* 'doc-string-elt 3) @@ -143,6 +147,7 @@ (put 'define-minor-mode 'doc-string-elt 2) (put 'easy-mmode-define-global-mode 'doc-string-elt 2) (put 'define-global-minor-mode 'doc-string-elt 2) +(put 'define-globalized-minor-mode 'doc-string-elt 2) (put 'define-generic-mode 'doc-string-elt 7) (put 'define-ibuffer-filter 'doc-string-elt 2) (put 'define-ibuffer-op 'doc-string-elt 3) @@ -151,6 +156,7 @@ (put 'defalias 'doc-string-elt 3) (put 'defvaralias 'doc-string-elt 3) (put 'define-category 'doc-string-elt 2) +(put 'define-overloadable-function 'doc-string-elt 3) (defvar lisp-doc-string-elt-property 'doc-string-elt "The symbol property that holds the docstring position info.") @@ -191,9 +197,12 @@ font-lock-string-face)))) font-lock-comment-face)) -;; The LISP-SYNTAX argument is used by code in inf-lisp.el and is -;; (uselessly) passed from pp.el, chistory.el, gnus-kill.el and score-mode.el -(defun lisp-mode-variables (&optional lisp-syntax) +(defun lisp-mode-variables (&optional lisp-syntax keywords-case-insensitive) + "Common initialization routine for lisp modes. +The LISP-SYNTAX argument is used by code in inf-lisp.el and is +\(uselessly) passed from pp.el, chistory.el, gnus-kill.el and +score-mode.el. KEYWORDS-CASE-INSENSITIVE non-nil means that for +font-lock keywords will not be case sensitive." (when lisp-syntax (set-syntax-table lisp-mode-syntax-table)) (setq local-abbrev-table lisp-mode-abbrev-table) @@ -212,10 +221,6 @@ ;;(set (make-local-variable 'adaptive-fill-mode) nil) (make-local-variable 'indent-line-function) (setq indent-line-function 'lisp-indent-line) - (make-local-variable 'indent-region-function) - (setq indent-region-function 'lisp-indent-region) - (make-local-variable 'parse-sexp-ignore-comments) - (setq parse-sexp-ignore-comments t) (make-local-variable 'outline-regexp) (setq outline-regexp ";;;\\(;* [^ \t\n]\\|###autoload\\)\\|(") (make-local-variable 'outline-level) @@ -241,9 +246,9 @@ (setq multibyte-syntax-as-symbol t) (set (make-local-variable 'syntax-begin-function) 'beginning-of-defun) (setq font-lock-defaults - '((lisp-font-lock-keywords + `((lisp-font-lock-keywords lisp-font-lock-keywords-1 lisp-font-lock-keywords-2) - nil nil (("+-*/.<>=!?$%_&~^:@" . "w")) nil + nil ,keywords-case-insensitive (("+-*/.<>=!?$%_&~^:@" . "w")) nil (font-lock-mark-block-function . mark-defun) (font-lock-syntactic-face-function . lisp-font-lock-syntactic-face-function)))) @@ -257,7 +262,6 @@ (defvar lisp-mode-shared-map (let ((map (make-sparse-keymap))) - (define-key map "\t" 'lisp-indent-line) (define-key map "\e\C-q" 'indent-sexp) (define-key map "\177" 'backward-delete-char-untabify) ;; This gets in the way when viewing a Lisp file in view-mode. As @@ -267,41 +271,124 @@ map) "Keymap for commands shared by all sorts of Lisp modes.") -(defvar emacs-lisp-mode-map () +(defvar emacs-lisp-mode-map + (let ((map (make-sparse-keymap "Emacs-Lisp")) + (menu-map (make-sparse-keymap "Emacs-Lisp")) + (lint-map (make-sparse-keymap)) + (prof-map (make-sparse-keymap)) + (tracing-map (make-sparse-keymap))) + (set-keymap-parent map lisp-mode-shared-map) + (define-key map "\e\t" 'completion-at-point) + (define-key map "\e\C-x" 'eval-defun) + (define-key map "\e\C-q" 'indent-pp-sexp) + (define-key map [menu-bar emacs-lisp] (cons (purecopy "Emacs-Lisp") menu-map)) + (define-key menu-map [eldoc] + `(menu-item ,(purecopy "Auto-Display Documentation Strings") eldoc-mode + :button (:toggle . (bound-and-true-p eldoc-mode)) + :help ,(purecopy "Display the documentation string for the item under cursor"))) + (define-key menu-map [checkdoc] + `(menu-item ,(purecopy "Check Documentation Strings") checkdoc + :help ,(purecopy "Check documentation strings for style requirements"))) + (define-key menu-map [re-builder] + `(menu-item ,(purecopy "Construct Regexp") re-builder + :help ,(purecopy "Construct a regexp interactively"))) + (define-key menu-map [tracing] (cons (purecopy "Tracing") tracing-map)) + (define-key tracing-map [tr-a] + `(menu-item ,(purecopy "Untrace All") untrace-all + :help ,(purecopy "Untrace all currently traced functions"))) + (define-key tracing-map [tr-uf] + `(menu-item ,(purecopy "Untrace Function...") untrace-function + :help ,(purecopy "Untrace function, and possibly activate all remaining advice"))) + (define-key tracing-map [tr-sep] menu-bar-separator) + (define-key tracing-map [tr-q] + `(menu-item ,(purecopy "Trace Function Quietly...") trace-function-background + :help ,(purecopy "Trace the function with trace output going quietly to a buffer"))) + (define-key tracing-map [tr-f] + `(menu-item ,(purecopy "Trace Function...") trace-function + :help ,(purecopy "Trace the function given as an argument"))) + (define-key menu-map [profiling] (cons (purecopy "Profiling") prof-map)) + (define-key prof-map [prof-restall] + `(menu-item ,(purecopy "Remove Instrumentation for All Functions") elp-restore-all + :help ,(purecopy "Restore the original definitions of all functions being profiled"))) + (define-key prof-map [prof-restfunc] + `(menu-item ,(purecopy "Remove Instrumentation for Function...") elp-restore-function + :help ,(purecopy "Restore an instrumented function to its original definition"))) + + (define-key prof-map [sep-rem] menu-bar-separator) + (define-key prof-map [prof-resall] + `(menu-item ,(purecopy "Reset Counters for All Functions") elp-reset-all + :help ,(purecopy "Reset the profiling information for all functions being profiled"))) + (define-key prof-map [prof-resfunc] + `(menu-item ,(purecopy "Reset Counters for Function...") elp-reset-function + :help ,(purecopy "Reset the profiling information for a function"))) + (define-key prof-map [prof-res] + `(menu-item ,(purecopy "Show Profiling Results") elp-results + :help ,(purecopy "Display current profiling results"))) + (define-key prof-map [prof-pack] + `(menu-item ,(purecopy "Instrument Package...") elp-instrument-package + :help ,(purecopy "Instrument for profiling all function that start with a prefix"))) + (define-key prof-map [prof-func] + `(menu-item ,(purecopy "Instrument Function...") elp-instrument-function + :help ,(purecopy "Instrument a function for profiling"))) + (define-key menu-map [lint] (cons (purecopy "Linting") lint-map)) + (define-key lint-map [lint-di] + `(menu-item ,(purecopy "Lint Directory...") elint-directory + :help ,(purecopy "Lint a directory"))) + (define-key lint-map [lint-f] + `(menu-item ,(purecopy "Lint File...") elint-file + :help ,(purecopy "Lint a file"))) + (define-key lint-map [lint-b] + `(menu-item ,(purecopy "Lint Buffer") elint-current-buffer + :help ,(purecopy "Lint the current buffer"))) + (define-key lint-map [lint-d] + `(menu-item ,(purecopy "Lint Defun") elint-defun + :help ,(purecopy "Lint the function at point"))) + (define-key menu-map [edebug-defun] + `(menu-item ,(purecopy "Instrument Function for Debugging") edebug-defun + :help ,(purecopy "Evaluate the top level form point is in, stepping through with Edebug") + :keys ,(purecopy "C-u C-M-x"))) + (define-key menu-map [separator-byte] menu-bar-separator) + (define-key menu-map [disas] + `(menu-item ,(purecopy "Disassemble Byte Compiled Object...") disassemble + :help ,(purecopy "Print disassembled code for OBJECT in a buffer"))) + (define-key menu-map [byte-recompile] + `(menu-item ,(purecopy "Byte-recompile Directory...") byte-recompile-directory + :help ,(purecopy "Recompile every `.el' file in DIRECTORY that needs recompilation"))) + (define-key menu-map [emacs-byte-compile-and-load] + `(menu-item ,(purecopy "Byte-compile and Load") emacs-lisp-byte-compile-and-load + :help ,(purecopy "Byte-compile the current file (if it has changed), then load compiled code"))) + (define-key menu-map [byte-compile] + `(menu-item ,(purecopy "Byte-compile This File") emacs-lisp-byte-compile + :help ,(purecopy "Byte compile the file containing the current buffer"))) + (define-key menu-map [separator-eval] menu-bar-separator) + (define-key menu-map [ielm] + `(menu-item ,(purecopy "Interactive Expression Evaluation") ielm + :help ,(purecopy "Interactively evaluate Emacs Lisp expressions"))) + (define-key menu-map [eval-buffer] + `(menu-item ,(purecopy "Evaluate Buffer") eval-buffer + :help ,(purecopy "Execute the current buffer as Lisp code"))) + (define-key menu-map [eval-region] + `(menu-item ,(purecopy "Evaluate Region") eval-region + :help ,(purecopy "Execute the region as Lisp code") + :enable mark-active)) + (define-key menu-map [eval-sexp] + `(menu-item ,(purecopy "Evaluate Last S-expression") eval-last-sexp + :help ,(purecopy "Evaluate sexp before point; print value in minibuffer"))) + (define-key menu-map [separator-format] menu-bar-separator) + (define-key menu-map [comment-region] + `(menu-item ,(purecopy "Comment Out Region") comment-region + :help ,(purecopy "Comment or uncomment each line in the region") + :enable mark-active)) + (define-key menu-map [indent-region] + `(menu-item ,(purecopy "Indent Region") indent-region + :help ,(purecopy "Indent each nonblank line in the region") + :enable mark-active)) + (define-key menu-map [indent-line] + `(menu-item ,(purecopy "Indent Line") lisp-indent-line)) + map) "Keymap for Emacs Lisp mode. All commands in `lisp-mode-shared-map' are inherited by this map.") -(if emacs-lisp-mode-map - () - (let ((map (make-sparse-keymap "Emacs-Lisp"))) - (setq emacs-lisp-mode-map (make-sparse-keymap)) - (set-keymap-parent emacs-lisp-mode-map lisp-mode-shared-map) - (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol) - (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun) - (define-key emacs-lisp-mode-map "\e\C-q" 'indent-pp-sexp) - (define-key emacs-lisp-mode-map [menu-bar] (make-sparse-keymap)) - (define-key emacs-lisp-mode-map [menu-bar emacs-lisp] - (cons "Emacs-Lisp" map)) - (define-key map [edebug-defun] - '("Instrument Function for Debugging" . edebug-defun)) - (define-key map [byte-recompile] - '("Byte-recompile Directory..." . byte-recompile-directory)) - (define-key map [emacs-byte-compile-and-load] - '("Byte-compile And Load" . emacs-lisp-byte-compile-and-load)) - (define-key map [byte-compile] - '("Byte-compile This File" . emacs-lisp-byte-compile)) - (define-key map [separator-eval] '("--")) - (define-key map [eval-buffer] '("Evaluate Buffer" . eval-current-buffer)) - (define-key map [eval-region] '("Evaluate Region" . eval-region)) - (define-key map [eval-sexp] '("Evaluate Last S-expression" . eval-last-sexp)) - (define-key map [separator-format] '("--")) - (define-key map [comment-region] '("Comment Out Region" . comment-region)) - (define-key map [indent-region] '("Indent Region" . indent-region)) - (define-key map [indent-line] '("Indent Line" . lisp-indent-line)) - (put 'eval-region 'menu-enable 'mark-active) - (put 'comment-region 'menu-enable 'mark-active) - (put 'indent-region 'menu-enable 'mark-active))) - (defun emacs-lisp-byte-compile () "Byte compile the file containing the current buffer." (interactive) @@ -319,10 +406,7 @@ All commands in `lisp-mode-shared-map' are inherited by this map.") (if (and (buffer-modified-p) (y-or-n-p (format "Save buffer %s first? " (buffer-name)))) (save-buffer)) - (let ((compiled-file-name (byte-compile-dest-file buffer-file-name))) - (if (file-newer-than-file-p compiled-file-name buffer-file-name) - (load-file compiled-file-name) - (byte-compile-file buffer-file-name t)))) + (byte-recompile-file buffer-file-name nil 0 t)) (defcustom emacs-lisp-mode-hook nil "Hook run when entering Emacs Lisp mode." @@ -342,60 +426,59 @@ All commands in `lisp-mode-shared-map' are inherited by this map.") :type 'hook :group 'lisp) -(defun emacs-lisp-mode () +(define-derived-mode emacs-lisp-mode prog-mode "Emacs-Lisp" "Major mode for editing Lisp code to run in Emacs. Commands: Delete converts tabs to spaces as it moves back. Blank lines separate paragraphs. Semicolons start comments. + \\{emacs-lisp-mode-map} Entry to this mode calls the value of `emacs-lisp-mode-hook' if that value is non-nil." - (interactive) - (kill-all-local-variables) - (use-local-map emacs-lisp-mode-map) - (set-syntax-table emacs-lisp-mode-syntax-table) - (setq major-mode 'emacs-lisp-mode) - (setq mode-name "Emacs-Lisp") + :group 'lisp (lisp-mode-variables) (setq imenu-case-fold-search nil) - (run-mode-hooks 'emacs-lisp-mode-hook)) -(put 'emacs-lisp-mode 'custom-mode-group 'lisp) + (add-hook 'completion-at-point-functions + 'lisp-completion-at-point nil 'local)) (defvar lisp-mode-map - (let ((map (make-sparse-keymap))) + (let ((map (make-sparse-keymap)) + (menu-map (make-sparse-keymap "Lisp"))) (set-keymap-parent map lisp-mode-shared-map) (define-key map "\e\C-x" 'lisp-eval-defun) (define-key map "\C-c\C-z" 'run-lisp) + (define-key map [menu-bar lisp] (cons (purecopy "Lisp") menu-map)) + (define-key menu-map [run-lisp] + `(menu-item ,(purecopy "Run inferior Lisp") run-lisp + :help ,(purecopy "Run an inferior Lisp process, input and output via buffer `*inferior-lisp*'"))) + (define-key menu-map [ev-def] + `(menu-item ,(purecopy "Eval defun") lisp-eval-defun + :help ,(purecopy "Send the current defun to the Lisp process made by M-x run-lisp"))) + (define-key menu-map [ind-sexp] + `(menu-item ,(purecopy "Indent sexp") indent-sexp + :help ,(purecopy "Indent each line of the list starting just after point"))) map) "Keymap for ordinary Lisp mode. All commands in `lisp-mode-shared-map' are inherited by this map.") -(defun lisp-mode () +(define-derived-mode lisp-mode prog-mode "Lisp" "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp. Commands: Delete converts tabs to spaces as it moves back. Blank lines separate paragraphs. Semicolons start comments. + \\{lisp-mode-map} Note that `run-lisp' may be used either to start an inferior Lisp job or to switch back to an existing one. Entry to this mode calls the value of `lisp-mode-hook' if that value is non-nil." - (interactive) - (kill-all-local-variables) - (use-local-map lisp-mode-map) - (setq major-mode 'lisp-mode) - (setq mode-name "Lisp") - (lisp-mode-variables) + (lisp-mode-variables nil t) + (set (make-local-variable 'find-tag-default-function) 'lisp-find-tag-default) (make-local-variable 'comment-start-skip) (setq comment-start-skip "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *") - (make-local-variable 'font-lock-keywords-case-fold-search) - (setq font-lock-keywords-case-fold-search t) - (setq imenu-case-fold-search t) - (set-syntax-table lisp-mode-syntax-table) - (run-mode-hooks 'lisp-mode-hook)) -(put 'lisp-mode 'find-tag-default-function 'lisp-find-tag-default) + (setq imenu-case-fold-search t)) (defun lisp-find-tag-default () (let ((default (find-tag-default))) @@ -414,17 +497,34 @@ if that value is non-nil." (error "Process lisp does not exist")) (defvar lisp-interaction-mode-map - (let ((map (make-sparse-keymap))) + (let ((map (make-sparse-keymap)) + (menu-map (make-sparse-keymap "Lisp-Interaction"))) (set-keymap-parent map lisp-mode-shared-map) (define-key map "\e\C-x" 'eval-defun) (define-key map "\e\C-q" 'indent-pp-sexp) - (define-key map "\e\t" 'lisp-complete-symbol) + (define-key map "\e\t" 'completion-at-point) (define-key map "\n" 'eval-print-last-sexp) + (define-key map [menu-bar lisp-interaction] (cons (purecopy "Lisp-Interaction") menu-map)) + (define-key menu-map [eval-defun] + `(menu-item ,(purecopy "Evaluate Defun") eval-defun + :help ,(purecopy "Evaluate the top-level form containing point, or after point"))) + (define-key menu-map [eval-print-last-sexp] + `(menu-item ,(purecopy "Evaluate and Print") eval-print-last-sexp + :help ,(purecopy "Evaluate sexp before point; print value into current buffer"))) + (define-key menu-map [edebug-defun-lisp-interaction] + `(menu-item ,(purecopy "Instrument Function for Debugging") edebug-defun + :help ,(purecopy "Evaluate the top level form point is in, stepping through with Edebug") + :keys ,(purecopy "C-u C-M-x"))) + (define-key menu-map [indent-pp-sexp] + `(menu-item ,(purecopy "Indent or Pretty-Print") indent-pp-sexp + :help ,(purecopy "Indent each line of the list starting just after point, or prettyprint it"))) + (define-key menu-map [complete-symbol] + `(menu-item ,(purecopy "Complete Lisp Symbol") completion-at-point + :help ,(purecopy "Perform completion on Lisp symbol preceding point"))) map) "Keymap for Lisp Interaction mode. All commands in `lisp-mode-shared-map' are inherited by this map.") -(defvar lisp-interaction-mode-abbrev-table lisp-mode-abbrev-table) (define-derived-mode lisp-interaction-mode emacs-lisp-mode "Lisp Interaction" "Major mode for typing and evaluating Lisp forms. Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression @@ -436,6 +536,7 @@ Commands: Delete converts tabs to spaces as it moves back. Paragraphs are separated only by blank lines. Semicolons start comments. + \\{lisp-interaction-mode-map} Entry to this mode calls the value of `lisp-interaction-mode-hook' if that value is non-nil.") @@ -490,6 +591,8 @@ alternative printed representations that can be displayed." (point (point))) (delete-region beg end) (insert (nth 1 value)) + (or (= beg point) + (setq point (1- (point)))) (last-sexp-setup-props beg (point) (nth 0 value) (nth 2 value) @@ -533,62 +636,72 @@ If CHAR is not a character, return nil." string)))) +(defun preceding-sexp () + "Return sexp before the point." + (let ((opoint (point)) + ignore-quotes + expr) + (save-excursion + (with-syntax-table emacs-lisp-mode-syntax-table + ;; If this sexp appears to be enclosed in `...' + ;; then ignore the surrounding quotes. + (setq ignore-quotes + (or (eq (following-char) ?\') + (eq (preceding-char) ?\'))) + (forward-sexp -1) + ;; If we were after `?\e' (or similar case), + ;; use the whole thing, not just the `e'. + (when (eq (preceding-char) ?\\) + (forward-char -1) + (when (eq (preceding-char) ??) + (forward-char -1))) + + ;; Skip over hash table read syntax. + (and (> (point) (1+ (point-min))) + (looking-back "#s" (- (point) 2)) + (forward-char -2)) + + ;; Skip over `#N='s. + (when (eq (preceding-char) ?=) + (let (labeled-p) + (save-excursion + (skip-chars-backward "0-9#=") + (setq labeled-p (looking-at "\\(#[0-9]+=\\)+"))) + (when labeled-p + (forward-sexp -1)))) + + (save-restriction + ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in + ;; `variable' so that the value is returned, not the + ;; name + (if (and ignore-quotes + (eq (following-char) ?`)) + (forward-char)) + (narrow-to-region (point-min) opoint) + (setq expr (read (current-buffer))) + ;; If it's an (interactive ...) form, it's more + ;; useful to show how an interactive call would + ;; use it. + (and (consp expr) + (eq (car expr) 'interactive) + (setq expr + (list 'call-interactively + (list 'quote + (list 'lambda + '(&rest args) + expr + 'args))))) + expr))))) + + (defun eval-last-sexp-1 (eval-last-sexp-arg-internal) "Evaluate sexp before point; print value in minibuffer. With argument, print output into current buffer." (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))) - (let ((value - (eval (let ((stab (syntax-table)) - (opoint (point)) - ignore-quotes - expr) - (save-excursion - (with-syntax-table emacs-lisp-mode-syntax-table - ;; If this sexp appears to be enclosed in `...' - ;; then ignore the surrounding quotes. - (setq ignore-quotes - (or (eq (following-char) ?\') - (eq (preceding-char) ?\'))) - (forward-sexp -1) - ;; If we were after `?\e' (or similar case), - ;; use the whole thing, not just the `e'. - (when (eq (preceding-char) ?\\) - (forward-char -1) - (when (eq (preceding-char) ??) - (forward-char -1))) - - ;; Skip over `#N='s. - (when (eq (preceding-char) ?=) - (let (labeled-p) - (save-excursion - (skip-chars-backward "0-9#=") - (setq labeled-p (looking-at "\\(#[0-9]+=\\)+"))) - (when labeled-p - (forward-sexp -1)))) - - (save-restriction - ;; vladimir@cs.ualberta.ca 30-Jul-1997: skip ` in - ;; `variable' so that the value is returned, not the - ;; name - (if (and ignore-quotes - (eq (following-char) ?`)) - (forward-char)) - (narrow-to-region (point-min) opoint) - (setq expr (read (current-buffer))) - ;; If it's an (interactive ...) form, it's more - ;; useful to show how an interactive call would - ;; use it. - (and (consp expr) - (eq (car expr) 'interactive) - (setq expr - (list 'call-interactively - (list 'quote - (list 'lambda - '(&rest args) - expr - 'args))))) - expr))))))) - (eval-last-sexp-print-value value)))) + ;; Setup the lexical environment if lexical-binding is enabled. + (eval-last-sexp-print-value + (eval (eval-sexp-add-defvars (preceding-sexp)) lexical-binding)))) + (defun eval-last-sexp-print-value (value) (let ((unabbreviated (let ((print-length nil) (print-level nil)) @@ -615,29 +728,48 @@ With argument, print output into current buffer." (defvar eval-last-sexp-fake-value (make-symbol "t")) +(defun eval-sexp-add-defvars (exp &optional pos) + "Prepend EXP with all the `defvar's that precede it in the buffer. +POS specifies the starting position where EXP was found and defaults to point." + (if (not lexical-binding) + exp + (save-excursion + (unless pos (setq pos (point))) + (let ((vars ())) + (goto-char (point-min)) + (while (re-search-forward + "^(def\\(?:var\\|const\\|custom\\)[ \t\n]+\\([^; '()\n\t]+\\)" + pos t) + (let ((var (intern (match-string 1)))) + (unless (special-variable-p var) + (push var vars)))) + `(progn ,@(mapcar (lambda (v) `(defvar ,v)) vars) ,exp))))) + (defun eval-last-sexp (eval-last-sexp-arg-internal) "Evaluate sexp before point; print value in minibuffer. Interactively, with prefix argument, print output into current buffer. +Truncates long output according to the value of the variables +`eval-expression-print-length' and `eval-expression-print-level'. If `eval-expression-debug-on-error' is non-nil, which is the default, this command arranges for all errors to enter the debugger." (interactive "P") (if (null eval-expression-debug-on-error) (eval-last-sexp-1 eval-last-sexp-arg-internal) - (let ((old-value eval-last-sexp-fake-value) new-value value) - (let ((debug-on-error old-value)) - (setq value (eval-last-sexp-1 eval-last-sexp-arg-internal)) - (setq new-value debug-on-error)) - (unless (eq old-value new-value) - (setq debug-on-error new-value)) - value))) + (let ((value + (let ((debug-on-error eval-last-sexp-fake-value)) + (cons (eval-last-sexp-1 eval-last-sexp-arg-internal) + debug-on-error)))) + (unless (eq (cdr value) eval-last-sexp-fake-value) + (setq debug-on-error (cdr value))) + (car value)))) (defun eval-defun-1 (form) "Treat some expressions specially. Reset the `defvar' and `defcustom' variables to the initial value. Reinitialize the face according to the `defface' specification." ;; The code in edebug-defun should be consistent with this, but not - ;; the same, since this gets a macroexpended form. + ;; the same, since this gets a macroexpanded form. (cond ((not (listp form)) form) ((and (eq (car form) 'defvar) @@ -649,30 +781,33 @@ Reinitialize the face according to the `defface' specification." ;; `defcustom' is now macroexpanded to ;; `custom-declare-variable' with a quoted value arg. ((and (eq (car form) 'custom-declare-variable) - (default-boundp (eval (nth 1 form)))) + (default-boundp (eval (nth 1 form) lexical-binding))) ;; Force variable to be bound. - (set-default (eval (nth 1 form)) (eval (nth 1 (nth 2 form)))) + (set-default (eval (nth 1 form) lexical-binding) + (eval (nth 1 (nth 2 form)) lexical-binding)) form) ;; `defface' is macroexpanded to `custom-declare-face'. ((eq (car form) 'custom-declare-face) ;; Reset the face. - (setq face-new-frame-defaults - (assq-delete-all (eval (nth 1 form)) face-new-frame-defaults)) - (put (eval (nth 1 form)) 'face-defface-spec nil) - ;; Setting `customized-face' to the new spec after calling - ;; the form, but preserving the old saved spec in `saved-face', - ;; imitates the situation when the new face spec is set - ;; temporarily for the current session in the customize - ;; buffer, thus allowing `face-user-default-spec' to use the - ;; new customized spec instead of the saved spec. - ;; Resetting `saved-face' temporarily to nil is needed to let - ;; `defface' change the spec, regardless of a saved spec. - (prog1 `(prog1 ,form - (put ,(nth 1 form) 'saved-face - ',(get (eval (nth 1 form)) 'saved-face)) - (put ,(nth 1 form) 'customized-face - ,(nth 2 form))) - (put (eval (nth 1 form)) 'saved-face nil))) + (let ((face-symbol (eval (nth 1 form) lexical-binding))) + (setq face-new-frame-defaults + (assq-delete-all face-symbol face-new-frame-defaults)) + (put face-symbol 'face-defface-spec nil) + (put face-symbol 'face-documentation (nth 3 form)) + ;; Setting `customized-face' to the new spec after calling + ;; the form, but preserving the old saved spec in `saved-face', + ;; imitates the situation when the new face spec is set + ;; temporarily for the current session in the customize + ;; buffer, thus allowing `face-user-default-spec' to use the + ;; new customized spec instead of the saved spec. + ;; Resetting `saved-face' temporarily to nil is needed to let + ;; `defface' change the spec, regardless of a saved spec. + (prog1 `(prog1 ,form + (put ,(nth 1 form) 'saved-face + ',(get face-symbol 'saved-face)) + (put ,(nth 1 form) 'customized-face + ,(nth 2 form))) + (put face-symbol 'saved-face nil)))) ((eq (car form) 'progn) (cons 'progn (mapcar 'eval-defun-1 (cdr form)))) (t form))) @@ -689,6 +824,8 @@ if it already has a value.\) With argument, insert value in current buffer after the defun. Return the result of evaluation." (interactive "P") + ;; FIXME: the print-length/level bindings should only be applied while + ;; printing, not while evaluating. (let ((debug-on-error eval-expression-debug-on-error) (print-length eval-expression-print-length) (print-level eval-expression-print-level)) @@ -706,7 +843,7 @@ Return the result of evaluation." (end-of-defun) (beginning-of-defun) (setq beg (point)) - (setq form (read (current-buffer))) + (setq form (eval-sexp-add-defvars (read (current-buffer)))) (setq end (point))) ;; Alter the form if necessary. (setq form (eval-defun-1 (macroexpand form))) @@ -726,7 +863,9 @@ If the current defun is actually a call to `defvar' or `defcustom', evaluating it this way resets the variable using its initial value expression even if the variable already has some other value. \(Normally `defvar' and `defcustom' do not alter the value if there -already is one.) +already is one.) In an analogous way, evaluating a `defface' +overrides any customizations of the face, so that it becomes +defined exactly as the `defface' expression says. If `eval-expression-debug-on-error' is non-nil, which is the default, this command arranges for all errors to enter the debugger. @@ -757,26 +896,25 @@ which see." value))))) ;; May still be used by some external Lisp-mode variant. -(define-obsolete-function-alias 'lisp-comment-indent 'comment-indent-default) - -;; This function just forces a more costly detection of comments (using -;; parse-partial-sexp from beginning-of-defun). I.e. It avoids the problem of -;; taking a `;' inside a string started on another line for a comment starter. -;; Note: `newcomment' gets it right now since we set comment-use-global-state -;; so we could get rid of it. -stef -(defun lisp-mode-auto-fill () - (if (> (current-column) (current-fill-column)) - (if (save-excursion - (nth 4 (syntax-ppss (point)))) - (do-auto-fill) - (unless (and (boundp 'comment-auto-fill-only-comments) - comment-auto-fill-only-comments) - (let ((comment-start nil) (comment-start-skip nil)) - (do-auto-fill)))))) - -(defvar lisp-indent-offset nil - "If non-nil, indent second line of expressions that many more columns.") -(defvar lisp-indent-function 'lisp-indent-function) +(define-obsolete-function-alias 'lisp-comment-indent + 'comment-indent-default "22.1") +(define-obsolete-function-alias 'lisp-mode-auto-fill 'do-auto-fill "23.1") + +(defcustom lisp-indent-offset nil + "If non-nil, indent second line of expressions that many more columns." + :group 'lisp + :type '(choice (const nil) integer)) +(put 'lisp-indent-offset 'safe-local-variable + (lambda (x) (or (null x) (integerp x)))) + +(defcustom lisp-indent-function 'lisp-indent-function + "A function to be called by `calculate-lisp-indent'. +It indents the arguments of a Lisp function call. This function +should accept two arguments: the indent-point, and the +`parse-partial-sexp' state at that position. One option for this +function is `common-lisp-indent-function'." + :type 'function + :group 'lisp) (defun lisp-indent-line (&optional whole-exp) "Indent current line as Lisp code. @@ -909,36 +1047,86 @@ is the buffer position of the start of the containing expression." ;; Indent by constant offset (goto-char containing-sexp) (+ (current-column) lisp-indent-offset)) + ;; in this case calculate-lisp-indent-last-sexp is not nil + (calculate-lisp-indent-last-sexp + (or + ;; try to align the parameters of a known function + (and lisp-indent-function + (not retry) + (funcall lisp-indent-function indent-point state)) + ;; If the function has no special alignment + ;; or it does not apply to this argument, + ;; try to align a constant-symbol under the last + ;; preceding constant symbol, if there is such one of + ;; the last 2 preceding symbols, in the previous + ;; uncommented line. + (and (save-excursion + (goto-char indent-point) + (skip-chars-forward " \t") + (looking-at ":")) + ;; The last sexp may not be at the indentation + ;; where it begins, so find that one, instead. + (save-excursion + (goto-char calculate-lisp-indent-last-sexp) + ;; Handle prefix characters and whitespace + ;; following an open paren. (Bug#1012) + (backward-prefix-chars) + (while (and (not (looking-back "^[ \t]*\\|([ \t]+")) + (or (not containing-sexp) + (< (1+ containing-sexp) (point)))) + (forward-sexp -1) + (backward-prefix-chars)) + (setq calculate-lisp-indent-last-sexp (point))) + (> calculate-lisp-indent-last-sexp + (save-excursion + (goto-char (1+ containing-sexp)) + (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) + (point))) + (let ((parse-sexp-ignore-comments t) + indent) + (goto-char calculate-lisp-indent-last-sexp) + (or (and (looking-at ":") + (setq indent (current-column))) + (and (< (line-beginning-position) + (prog2 (backward-sexp) (point))) + (looking-at ":") + (setq indent (current-column)))) + indent)) + ;; another symbols or constants not preceded by a constant + ;; as defined above. + normal-indent)) + ;; in this case calculate-lisp-indent-last-sexp is nil (desired-indent) - ((and (boundp 'lisp-indent-function) - lisp-indent-function - (not retry)) - (or (funcall lisp-indent-function indent-point state) - normal-indent)) (t normal-indent)))))) (defun lisp-indent-function (indent-point state) "This function is the normal value of the variable `lisp-indent-function'. -It is used when indenting a line within a function call, to see if the -called function says anything special about how to indent the line. +The function `calculate-lisp-indent' calls this to determine +if the arguments of a Lisp function call should be indented specially. INDENT-POINT is the position where the user typed TAB, or equivalent. Point is located at the point to indent under (for default indentation); STATE is the `parse-partial-sexp' state for that position. -If the current line is in a call to a Lisp function -which has a non-nil property `lisp-indent-function', -that specifies how to do the indentation. The property value can be -* `defun', meaning indent `defun'-style; +If the current line is in a call to a Lisp function that has a non-nil +property `lisp-indent-function' (or the deprecated `lisp-indent-hook'), +it specifies how to indent. The property value can be: + +* `defun', meaning indent `defun'-style + \(this is also the case if there is no property and the function + has a name that begins with \"def\", and three or more arguments); + * an integer N, meaning indent the first N arguments specially - like ordinary function arguments and then indent any further + (like ordinary function arguments), and then indent any further arguments like a body; -* a function to call just as this function was called. - If that function returns nil, that means it doesn't specify - the indentation. -This function also returns nil meaning don't specify the indentation." +* a function to call that returns the indentation (or nil). + `lisp-indent-function' calls this function with the same two arguments + that it itself received. + +This function returns either the indentation to use, or nil if the +Lisp function does not specify a special indentation." (let ((normal-indent (current-column))) (goto-char (1+ (elt state 1))) (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t) @@ -974,8 +1162,11 @@ This function also returns nil meaning don't specify the indentation." (method (funcall method indent-point state))))))) -(defvar lisp-body-indent 2 - "Number of columns to indent the second line of a `(def...)' form.") +(defcustom lisp-body-indent 2 + "Number of columns to indent the second line of a `(def...)' form." + :group 'lisp + :type 'integer) +(put 'lisp-body-indent 'safe-local-variable 'integerp) (defun lisp-indent-specform (count state indent-point normal-indent) (let ((containing-form-start (elt state 1)) @@ -1041,32 +1232,17 @@ This function also returns nil meaning don't specify the indentation." (put 'prog1 'lisp-indent-function 1) (put 'prog2 'lisp-indent-function 2) (put 'save-excursion 'lisp-indent-function 0) -(put 'save-window-excursion 'lisp-indent-function 0) -(put 'save-selected-window 'lisp-indent-function 0) (put 'save-restriction 'lisp-indent-function 0) (put 'save-match-data 'lisp-indent-function 0) (put 'save-current-buffer 'lisp-indent-function 0) -(put 'with-current-buffer 'lisp-indent-function 1) -(put 'combine-after-change-calls 'lisp-indent-function 0) -(put 'with-output-to-string 'lisp-indent-function 0) -(put 'with-temp-file 'lisp-indent-function 1) -(put 'with-temp-buffer 'lisp-indent-function 0) -(put 'with-temp-message 'lisp-indent-function 1) -(put 'with-syntax-table 'lisp-indent-function 1) (put 'let 'lisp-indent-function 1) (put 'let* 'lisp-indent-function 1) (put 'while 'lisp-indent-function 1) (put 'if 'lisp-indent-function 2) -(put 'read-if 'lisp-indent-function 2) (put 'catch 'lisp-indent-function 1) (put 'condition-case 'lisp-indent-function 2) (put 'unwind-protect 'lisp-indent-function 1) (put 'with-output-to-temp-buffer 'lisp-indent-function 1) -(put 'eval-after-load 'lisp-indent-function 1) -(put 'dolist 'lisp-indent-function 1) -(put 'dotimes 'lisp-indent-function 1) -(put 'when 'lisp-indent-function 1) -(put 'unless 'lisp-indent-function 1) (defun indent-sexp (&optional endpos) "Indent each line of the list starting just after point. @@ -1126,19 +1302,25 @@ ENDPOS is encountered." (make-list (- next-depth) nil)) last-depth (- last-depth next-depth) next-depth 0))) - (or outer-loop-done endpos - (setq outer-loop-done (<= next-depth 0))) - (if outer-loop-done - (forward-line 1) + (forward-line 1) + ;; Decide whether to exit. + (if endpos + ;; If we have already reached the specified end, + ;; give up and do not reindent this line. + (if (<= endpos (point)) + (setq outer-loop-done t)) + ;; If no specified end, we are done if we have finished one sexp. + (if (<= next-depth 0) + (setq outer-loop-done t))) + (unless outer-loop-done (while (> last-depth next-depth) (setq indent-stack (cdr indent-stack) last-depth (1- last-depth))) (while (< last-depth next-depth) (setq indent-stack (cons nil indent-stack) last-depth (1+ last-depth))) - ;; Now go to the next line and indent it according + ;; Now indent the next line according ;; to what we learned from parsing the previous one. - (forward-line 1) (setq bol (point)) (skip-chars-forward " \t") ;; But not if the line is blank, or just a comment @@ -1165,16 +1347,6 @@ ENDPOS is encountered." (setq outer-loop-done (= (point) last-point)) (setq last-point (point))))))) -(defun lisp-indent-region (start end) - "Indent every line whose first char is between START and END inclusive." - (save-excursion - (let ((endmark (copy-marker end))) - (goto-char start) - (and (bolp) (not (eolp)) - (lisp-indent-line)) - (indent-sexp endmark) - (set-marker endmark nil)))) - (defun indent-pp-sexp (&optional arg) "Indent each line of the list starting just after point, or prettyprint it. A prefix argument specifies pretty-printing." @@ -1239,7 +1411,8 @@ and initial semicolons." "\\|\\s-*\\([(;:\"]\\|`(\\|#'(\\)")) (paragraph-separate (concat paragraph-separate "\\|\\s-*\".*[,\\.]$")) - (fill-column (if (integerp emacs-lisp-docstring-fill-column) + (fill-column (if (and (integerp emacs-lisp-docstring-fill-column) + (derived-mode-p 'emacs-lisp-mode)) emacs-lisp-docstring-fill-column fill-column))) (fill-paragraph justify)) @@ -1281,5 +1454,4 @@ means don't indent that line." (provide 'lisp-mode) -;; arch-tag: 414c7f93-c245-4b77-8ed5-ed05ef7ff1bf ;;; lisp-mode.el ends here