Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / emacs-lisp / lisp.el
index ba15469..deb06f5 100644 (file)
@@ -1,10 +1,10 @@
 ;;; lisp.el --- Lisp editing commands for Emacs
 
-;; Copyright (C) 1985, 1986, 1994, 2000, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+;; Copyright (C) 1985-1986, 1994, 2000-2011 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; Keywords: lisp, languages
+;; Package: emacs
 
 ;; This file is part of GNU Emacs.
 
@@ -30,7 +30,7 @@
 
 ;; Note that this variable is used by non-lisp modes too.
 (defcustom defun-prompt-regexp nil
-  "*If non-nil, a regexp to ignore before a defun.
+  "If non-nil, a regexp to ignore before a defun.
 This is only necessary if the opening paren or brace is not in column 0.
 See function `beginning-of-defun'."
   :type '(choice (const nil)
@@ -140,9 +140,19 @@ A negative argument means move backward but still to a less deep spot.
 This command assumes point is not in a string or comment."
   (interactive "^p")
   (or arg (setq arg 1))
-  (let ((inc (if (> arg 0) 1 -1)))
+  (let ((inc (if (> arg 0) 1 -1))
+        pos)
     (while (/= arg 0)
-      (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
+      (if (null forward-sexp-function)
+          (goto-char (or (scan-lists (point) inc 1) (buffer-end arg)))
+          (condition-case err
+              (while (progn (setq pos (point))
+                       (forward-sexp inc)
+                       (/= (point) pos)))
+            (scan-error (goto-char (nth 2 err))))
+        (if (= (point) pos)
+            (signal 'scan-error
+                    (list "Unbalanced parentheses" (point) (point)))))
       (setq arg (- arg inc)))))
 
 (defun kill-sexp (&optional arg)
@@ -259,7 +269,8 @@ is called as a function to find the defun's beginning."
                                         "\\(?:" defun-prompt-regexp "\\)\\s(")
                               "^\\s(")
                             nil 'move arg)
-        (progn (goto-char (1- (match-end 0)))) t))
+        (progn (goto-char (1- (match-end 0)))
+                t)))
 
    ;; If open-paren-in-column-0-is-defun-start and defun-prompt-regexp
    ;; are both nil, column 0 has no significance - so scan forward
@@ -312,11 +323,13 @@ is called as a function to find the defun's beginning."
             (goto-char (if arg-+ve floor ceiling))
             nil))))))))
 
-(defvar end-of-defun-function #'forward-sexp
+(defvar end-of-defun-function
+  (lambda () (forward-sexp 1))
   "Function for `end-of-defun' to call.
-This is used to find the end of the defun.
+This is used to find the end of the defun at point.
 It is called with no argument, right after calling `beginning-of-defun-raw'.
-So the function can assume that point is at the beginning of the defun body.")
+So the function can assume that point is at the beginning of the defun body.
+It should move point to the first position after the defun.")
 
 (defun buffer-end (arg)
   "Return the \"far end\" position of the buffer, in direction ARG.
@@ -341,48 +354,44 @@ is called as a function to find the defun's end."
       (and transient-mark-mode mark-active)
       (push-mark))
   (if (or (null arg) (= arg 0)) (setq arg 1))
-  (while (> arg 0)
-    (let ((pos (point))
-         retry-point)
-      (end-of-line 1)
-      (beginning-of-defun-raw 1)
-      (while (unless (eobp)
-               (funcall end-of-defun-function)
-               (skip-chars-forward " \t")
-               (if (looking-at "\\s<\\|\n")
-                   (forward-line 1))
-               ;; If we started after the end of the previous
-               ;; function, try again with the next one.
-               (unless (or (> (point) pos)
-                          (eq (point) retry-point))
-                (or (bobp) (forward-char -1))
-                (beginning-of-defun-raw -1)
-                (setq retry-point (point)))))
-      ;; Ensure that we move forward.
-      (when (< (point) pos)
-       (goto-char pos)))
-    (setq arg (1- arg)))
-  (while (< arg 0)
-    (let ((pos (point)))
-      (while (unless (bobp)
-               (beginning-of-line 1)
-               (beginning-of-defun-raw 1)
-               (let ((beg (point))
-                    retry-point)
-                 (funcall end-of-defun-function)
-                 (skip-chars-forward " \t")
-                 (if (looking-at "\\s<\\|\n")
-                     (forward-line 1))
-                 ;; If we started from within the function just found,
-                 ;; try again with the previous one.
-                 (unless (or (< (point) pos)
-                            (eq (point) retry-point))
-                   (goto-char beg)
-                   (setq retry-point (point))))))
-      ;; Ensure that we move backward.
-      (when (> (point) pos)
-       (goto-char pos)))
-    (setq arg (1+ arg))))
+  (let ((pos (point))
+        (beg (progn (end-of-line 1) (beginning-of-defun-raw 1) (point))))
+    (funcall end-of-defun-function)
+    ;; When comparing point against pos, we want to consider that if
+    ;; point was right after the end of the function, it's still
+    ;; considered as "in that function".
+    ;; E.g. `eval-defun' from right after the last close-paren.
+    (unless (bolp)
+      (skip-chars-forward " \t")
+      (if (looking-at "\\s<\\|\n")
+          (forward-line 1)))
+    (cond
+     ((> arg 0)
+      ;; Moving forward.
+      (if (> (point) pos)
+          ;; We already moved forward by one because we started from
+          ;; within a function.
+          (setq arg (1- arg))
+        ;; We started from after the end of the previous function.
+        (goto-char pos))
+      (unless (zerop arg)
+        (beginning-of-defun-raw (- arg))
+        (funcall end-of-defun-function)))
+     ((< arg 0)
+      ;; Moving backward.
+      (if (< (point) pos)
+          ;; We already moved backward because we started from between
+          ;; two functions.
+          (setq arg (1+ arg))
+        ;; We started from inside a function.
+        (goto-char beg))
+      (unless (zerop arg)
+        (beginning-of-defun-raw (- arg))
+        (funcall end-of-defun-function))))
+    (unless (bolp)
+      (skip-chars-forward " \t")
+      (if (looking-at "\\s<\\|\n")
+          (forward-line 1)))))
 
 (defun mark-defun (&optional allow-extend)
   "Put mark at end of this defun, point at beginning.
@@ -598,43 +607,16 @@ character."
                (error "Unmatched bracket or quote"))))
 \f
 (defun field-complete (table &optional predicate)
-  (let* ((pattern (field-string-no-properties))
-         (completion (try-completion pattern table predicate)))
-    (cond ((eq completion t))
-          ((null completion)
-           (message "Can't find completion for \"%s\"" pattern)
-           (ding))
-          ((not (string= pattern completion))
-           (delete-region (field-beginning) (field-end))
-           (insert completion)
-           ;; Don't leave around a completions buffer that's out of date.
-           (let ((win (get-buffer-window "*Completions*" 0)))
-             (if win (with-selected-window win (bury-buffer)))))
-          (t
-           (let ((minibuf-is-in-use
-                  (eq (minibuffer-window) (selected-window))))
-             (unless minibuf-is-in-use
-               (message "Making completion list..."))
-             (let ((list (all-completions pattern table predicate)))
-               (setq list (sort list 'string<))
-               (or (eq predicate 'fboundp)
-                   (let (new)
-                     (while list
-                       (setq new (cons (if (fboundp (intern (car list)))
-                                           (list (car list) " <f>")
-                                         (car list))
-                                       new))
-                       (setq list (cdr list)))
-                     (setq list (nreverse new))))
-               (if (> (length list) 1)
-                   (with-output-to-temp-buffer "*Completions*"
-                     (display-completion-list list pattern))
-                 ;; Don't leave around a completions buffer that's
-                 ;; out of date.
-                 (let ((win (get-buffer-window "*Completions*" 0)))
-                   (if win (with-selected-window win (bury-buffer))))))
-             (unless minibuf-is-in-use
-               (message "Making completion list...%s" "done")))))))
+  (let ((minibuffer-completion-table table)
+        (minibuffer-completion-predicate predicate)
+        ;; This made sense for lisp-complete-symbol, but for
+        ;; field-complete, this is out of place.  --Stef
+        ;; (completion-annotate-function
+        ;;  (unless (eq predicate 'fboundp)
+        ;;    (lambda (str)
+        ;;      (if (fboundp (intern-soft str)) " <f>"))))
+        )
+    (call-interactively 'minibuffer-complete)))
 
 (defun lisp-complete-symbol (&optional predicate)
   "Perform completion on Lisp symbol preceding point.
@@ -650,85 +632,61 @@ symbols with function definitions are considered.  Otherwise, all
 symbols with function definitions, values or properties are
 considered."
   (interactive)
-  (let ((window (get-buffer-window "*Completions*" 0)))
-    (if (and (eq last-command this-command)
-            window (window-live-p window) (window-buffer window)
-            (buffer-name (window-buffer window)))
-       ;; If this command was repeated, and
-       ;; there's a fresh completion window with a live buffer,
-       ;; and this command is repeated, scroll that window.
-       (with-current-buffer (window-buffer window)
-         (if (pos-visible-in-window-p (point-max) window)
-             (set-window-start window (point-min))
-           (save-selected-window
-             (select-window window)
-             (scroll-up))))
-
-      ;; Do completion.
-      (let* ((end (point))
-            (beg (with-syntax-table emacs-lisp-mode-syntax-table
+  (let* ((data (lisp-completion-at-point predicate))
+         (plist (nthcdr 3 data)))
+    (if (null data)
+        (minibuffer-message "Nothing to complete")
+      (let ((completion-annotate-function
+             (plist-get plist :annotate-function)))
+      (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
+                              (plist-get plist :predicate))))))
+
+
+(defun lisp-completion-at-point (&optional predicate)
+  "Function used for `completion-at-point-functions' in `emacs-lisp-mode'."
+  ;; FIXME: the `end' could be after point?
+  (with-syntax-table emacs-lisp-mode-syntax-table
+    (let* ((pos (point))
+          (beg (condition-case nil
                    (save-excursion
                      (backward-sexp 1)
-                     (while (= (char-syntax (following-char)) ?\')
-                       (forward-char 1))
-                     (point))))
-            (pattern (buffer-substring-no-properties beg end))
-            (predicate
-             (or predicate
+                     (skip-syntax-forward "'")
+                     (point))
+                 (scan-error pos)))
+          (predicate
+           (or predicate
+               (save-excursion
+                 (goto-char beg)
+                 (if (not (eq (char-before) ?\())
+                     (lambda (sym)          ;why not just nil ?   -sm
+                       (or (boundp sym) (fboundp sym)
+                           (symbol-plist sym)))
+                   ;; Looks like a funcall position.  Let's double check.
+                   (if (condition-case nil
+                           (progn (up-list -2) (forward-char 1)
+                                  (eq (char-after) ?\())
+                         (error nil))
+                       ;; If the first element of the parent list is an open
+                       ;; paren we are probably not in a funcall position.
+                       ;; Maybe a `let' varlist or something.
+                       nil
+                     ;; Else, we assume that a function name is expected.
+                     'fboundp)))))
+          (end
+           (unless (or (eq beg (point-max))
+                       (member (char-syntax (char-after beg)) '(?\" ?\( ?\))))
+             (condition-case nil
                  (save-excursion
                    (goto-char beg)
-                   (if (not (eq (char-before) ?\())
-                       (lambda (sym)   ;why not just nil ?   -sm
-                         (or (boundp sym) (fboundp sym)
-                             (symbol-plist sym)))
-                     ;; Looks like a funcall position.  Let's double check.
-                     (if (condition-case nil
-                             (progn (up-list -2) (forward-char 1)
-                                    (eq (char-after) ?\())
-                           (error nil))
-                         ;; If the first element of the parent list is an open
-                         ;; parenthesis we are probably not in a funcall position.
-                         ;; Maybe a `let' varlist or something.
-                         nil
-                       ;; Else, we assume that a function name is expected.
-                       'fboundp)))))
-            (completion (try-completion pattern obarray predicate)))
-       (cond ((eq completion t))
-             ((null completion)
-              (if (window-minibuffer-p (selected-window))
-                  (minibuffer-message (format " [No completions of \"%s\"]" pattern))
-                (message "Can't find completion for \"%s\"" pattern))
-              (ding))
-             ((not (string= pattern completion))
-              (delete-region beg end)
-              (insert completion)
-              ;; Don't leave around a completions buffer that's out of date.
-              (let ((win (get-buffer-window "*Completions*" 0)))
-                (if win (with-selected-window win (bury-buffer)))))
-             (t
-              (let ((minibuf-is-in-use
-                     (eq (minibuffer-window) (selected-window))))
-                (unless minibuf-is-in-use
-                  (message "Making completion list..."))
-                (let ((list (all-completions pattern obarray predicate)))
-                  (setq list (sort list 'string<))
-                  (unless (eq predicate 'fboundp)
-                    (let (new)
-                      (dolist (compl list)
-                        (push (if (fboundp (intern compl))
-                                  (list compl " <f>")
-                                compl)
-                              new))
-                      (setq list (nreverse new))))
-                  (if (> (length list) 1)
-                      (with-output-to-temp-buffer "*Completions*"
-                        (display-completion-list list pattern))
-                    ;; Don't leave around a completions buffer that's
-                    ;; out of date.
-                    (let ((win (get-buffer-window "*Completions*" 0)))
-                      (if win (with-selected-window win (bury-buffer))))))
-                (unless minibuf-is-in-use
-                  (message "Making completion list...%s" "done")))))))))
-
-;; arch-tag: aa7fa8a4-2e6f-4e9b-9cd9-fef06340e67e
+                   (forward-sexp 1)
+                   (when (>= (point) pos)
+                     (point)))
+               (scan-error pos)))))
+      (when end
+       (list beg end obarray
+             :predicate predicate
+             :annotate-function
+             (unless (eq predicate 'fboundp)
+               (lambda (str) (if (fboundp (intern-soft str)) " <f>"))))))))
+
 ;;; lisp.el ends here