]> code.delx.au - gnu-emacs/blobdiff - lisp/iswitchb.el
(turn-off-xterm-mouse-tracking): Doc fix.
[gnu-emacs] / lisp / iswitchb.el
index b34c8fee70ebcfd31b7c222c8d8ed1c3cf55e93f..5ec032adb9841107de4dd330feb7806b252f3945 100644 (file)
@@ -1,9 +1,9 @@
 ;;; iswitchb.el --- switch between buffers using substrings
 
-;; Copyright (C) 1996, 1997, 2000  Free Software Foundation, Inc.
+;; Copyright (C) 1996, 1997, 2000, 2001  Free Software Foundation, Inc.
 
-;; Author: Stephen Eglen <stephen@anc.ed.ac.uk>
-;; Maintainer: Stephen Eglen <stephen@anc.ed.ac.uk>
+;; Author: Stephen Eglen <stephen@gnu.org>
+;; Maintainer: Stephen Eglen <stephen@gnu.org>
 ;; Keywords: completion convenience
 ;; location: http://www.anc.ed.ac.uk/~stephen/emacs/
 
 ;; See the User Variables section below for easy ways to change the
 ;; functionality of the program.  These are accessible using the
 ;; custom package.
-;; To modify the keybindings, use the hook provided.  For example:
-;;(add-hook 'iswitchb-define-mode-map-hook
-;;       'iswitchb-my-keys)
+;; To modify the keybindings, use something like:
 ;;
+;;(add-hook 'iswitchb-mode-hook 'iswitchb-my-keys)
 ;;(defun iswitchb-my-keys ()
 ;;  "Add my keybindings for iswitchb."
-;;  (define-key iswitchb-mode-map " " 'iswitchb-next-match)
-;;  )
+;;  (define-key iswitchb-mode-map " " 'iswitchb-next-match))
 ;;
 ;; Seeing all the matching buffers
 ;;
 ;; If you have many matching buffers, they may not all fit onto one
-;; line of the minibuffer.  In this case, you should use rsz-mini
-;; (resize-minibuffer-mode).  You can also limit iswitchb so that it
+;; line of the minibuffer.  In Emacs 21, the variable
+;; `resize-mini-windows' controls how many lines of the minibuffer can
+;; be seen.  For older versions of emacs, you can use
+;; `resize-minibuffer-mode'.  You can also limit iswitchb so that it
 ;; only shows a certain number of lines -- see the documentation for
 ;; `iswitchb-minibuffer-setup-hook'.
 
   :link '(url-link "http://www.anc.ed.ac.uk/~stephen/emacs/")
   :link '(emacs-library-link :tag "Lisp File" "iswitchb.el"))
 
-;;;###autoload
-(defcustom iswitchb-mode nil
-  "Toggle Iswitchb mode.
-Setting this variable directly does not take effect;
-use either \\[customize] or the function `iswitchb-mode'."
-  :set (lambda (symbol value)
-        (iswitchb-mode (or value 0)))
-  :initialize 'custom-initialize-default
-  :group 'iswitchb
-  :version "21.1"
-  :type 'boolean)
-
-(defcustom iswitchb-mode-hook nil
-  "Hook run at the end of function `iswitchb-mode'."
-  :group 'iswitchb
-  :type 'hook)
-
 (defcustom iswitchb-case case-fold-search
   "*Non-nil if searching of buffer names should ignore case.
 If this is non-nil but the user input has any upper case letters, matching
@@ -268,6 +251,14 @@ example functions that filter buffernames."
   :type '(repeat regexp)
   :group 'iswitchb)
 
+(defcustom iswitchb-cannot-complete-hook 'iswitchb-completion-help
+  "*Hook run when `iswitchb-complete' can't complete any more.
+The most useful values are `iswitchb-completion-help', which pops up a
+window with completion alternatives, or `iswitchb-next-match' or
+`iswitchb-prev-match', which cycle the buffer list."
+  :type 'hook
+  :group 'iswitchb)
+
 ;;; Examples for setting the value of iswitchb-buffer-ignore
 ;(defun iswitchb-ignore-c-mode (name)
 ;  "Ignore all c mode buffers -- example function for iswitchb."
@@ -316,11 +307,6 @@ See also `iswitchb-newbuffer'."
   :type 'boolean
   :group 'iswitchb)
 
-(defcustom iswitchb-define-mode-map-hook  nil
-  "Hook to define keys in `iswitchb-mode-map' for extra keybindings."
-  :type 'hook
-  :group 'iswitchb)
-
 (defcustom iswitchb-use-fonts t
   "*Non-nil means use font-lock fonts for showing first match."
   :type 'boolean
@@ -458,6 +444,9 @@ selected.")
 (defvar iswitchb-bufs-in-frame nil
   "List of the buffers visible in the current frame.")
 
+(defvar iswitchb-minibuf-depth nil
+  "Value we expect to be returned by `minibuffer-depth' in the minibuffer.")
+
 ;;; FUNCTIONS
 
 ;;; ISWITCHB KEYMAP
@@ -514,12 +503,8 @@ in a separate window.
   ;;\\[iswitchb-toggle-ignore] Toggle ignoring certain buffers (see \
   ;;`iswitchb-buffer-ignore')
        
-  (let
-      (prompt buf)
-    
-    (setq prompt (format "iswitch "))
-
-    (setq buf (iswitchb-read-buffer prompt))
+  (let* ((prompt "iswitch ")
+        (buf (iswitchb-read-buffer prompt)))
 
     ;;(message "chosen text %s" iswitchb-final-text)
     ;; Choose the buffer name: either the text typed in, or the head
@@ -570,15 +555,22 @@ If REQUIRE-MATCH is non-nil, an existing-buffer must be selected."
     (let
        ((minibuffer-local-completion-map iswitchb-mode-map)
         (iswitchb-prepost-hooks t)
+        ;; Record the minibuffer depth that we expect to find once
+        ;; the minibuffer is set up and iswitchb-entryfn-p is called.
+        (iswitchb-minibuf-depth (1+ (minibuffer-depth)))
         (iswitchb-require-match require-match))
       ;; prompt the user for the buffer name
       (setq iswitchb-final-text (completing-read
                                 prompt ;the prompt
-                                '(("dummy".1)) ;table
+                                '(("dummy" . 1)) ;table
                                 nil    ;predicate
                                 nil    ;require-match [handled elsewhere]
                                 nil    ;initial-contents
                                 'iswitchb-history)))
+    (if (get-buffer iswitchb-final-text)
+       ;; This happens for example if the buffer was chosen with the mouse.
+       (setq iswitchb-matches (list iswitchb-final-text)))
+
     ;; Handling the require-match must be done in a better way.
     (if (and require-match (not (iswitchb-existing-buffer-p)))
        (error "Must specify valid buffer"))
@@ -623,7 +615,7 @@ The result is stored in `iswitchb-common-match-string'."
   (interactive)
   (let (res)
     (cond ((not  iswitchb-matches)
-          (iswitchb-completion-help))
+          (run-hooks 'iswitchb-cannot-complete-hook))
          
          ((= 1 (length iswitchb-matches))
           ;; only one choice, so select it.
@@ -640,7 +632,7 @@ The result is stored in `iswitchb-common-match-string'."
                 (delete-region (minibuffer-prompt-end) (point))
                 (insert  res))
             ;; else nothing to complete
-            (iswitchb-completion-help)
+            (run-hooks 'iswitchb-cannot-complete-hook)
             )))))
 
 ;;; TOGGLE FUNCTIONS
@@ -732,21 +724,22 @@ in this list.  If DEFAULT is non-nil, and corresponds to an existing buffer,
 it is put to the start of the list."
   (setq iswitchb-buflist
        (let* ((iswitchb-current-buffers (iswitchb-get-buffers-in-frames))
-             (iswitchb-temp-buflist
-              (delq nil
-                    (mapcar
-                     (lambda (x)
-                       (let ((b-name (buffer-name x)))
-                         (if (not
-                              (or
-                               (iswitchb-ignore-buffername-p b-name)
-                               (memq b-name iswitchb-current-buffers)))
-                             b-name)))
-                     (buffer-list (and iswitchb-use-frame-buffer-list
-                                       (selected-frame)))))))
-         (nconc iswitchb-temp-buflist iswitchb-current-buffers)
+              (iswitchb-temp-buflist
+               (delq nil
+                     (mapcar
+                      (lambda (x)
+                        (let ((b-name (buffer-name x)))
+                          (if (not
+                               (or
+                                (iswitchb-ignore-buffername-p b-name)
+                                (memq b-name iswitchb-current-buffers)))
+                              b-name)))
+                      (buffer-list (and iswitchb-use-frame-buffer-list
+                                        (selected-frame)))))))
+         (setq iswitchb-temp-buflist
+               (nconc iswitchb-temp-buflist iswitchb-current-buffers))
          (run-hooks 'iswitchb-make-buflist-hook)
-         ;; Should this be after the hooks, or should the hooks be the
+        ;; Should this be after the hooks, or should the hooks be the
          ;; final thing to be run?
          (if default
              (progn
@@ -762,7 +755,7 @@ it is put to the start of the list."
    (lambda (elem)
      (setq iswitchb-temp-buflist (delq elem iswitchb-temp-buflist)))
    lst)
-  (nconc iswitchb-temp-buflist lst))
+  (setq iswitchb-temp-buflist (nconc iswitchb-temp-buflist lst)))
 
 (defun iswitchb-get-buffers-in-frames (&optional current)
   "Return the list of buffers that are visible in the current frame.
@@ -897,19 +890,17 @@ Return the modified list with the last element prepended to it."
       (cons (car las) lis))))
 
 (defun iswitchb-completion-help ()
-  "Show possible completions in a *Buffer Completions* buffer."
+  "Show possible completions in a *Completions* buffer."
   ;; we could allow this buffer to be used to select match, but I think
   ;; choose-completion-string will need redefining, so it just inserts
   ;; choice with out any previous input.
   (interactive)
   (setq iswitchb-rescan nil)
-  (let ((completion-setup-hook nil)    ;disable fancy highlight/selection.
-       (buf (current-buffer))
-       (temp-buf "*Buffer Completions*")
-       (win)
-       (again (eq last-command this-command)))
+  (let ((buf (current-buffer))
+       (temp-buf "*Completions*")
+       (win))
 
-    (if again
+    (if (eq last-command this-command)
        ;; scroll buffer
        (progn
          (set-buffer temp-buf)
@@ -1152,11 +1143,11 @@ Copied from `icomplete-exhibit' with two changes:
          (iswitchb-set-common-completion)
 
          ;; Insert the match-status information:
-         (insert-string (iswitchb-completions 
-                         contents
-                         minibuffer-completion-table
-                         minibuffer-completion-predicate
-                         (not minibuffer-completion-confirm)))))))
+         (insert (iswitchb-completions 
+                  contents
+                  minibuffer-completion-table
+                  minibuffer-completion-predicate
+                  (not minibuffer-completion-confirm)))))))
 
 (defun iswitchb-completions (name candidates predicate require-match)
   "Return the string that is displayed after the user's text.
@@ -1248,21 +1239,11 @@ Modified from `icomplete-completions'."
 (defun iswitchb-minibuffer-setup ()
   "Set up minibuffer for `iswitchb-buffer'.
 Copied from `icomplete-minibuffer-setup-hook'."
-  (if (iswitchb-entryfn-p)
-      (progn
-
-       (make-local-variable 'iswitchb-use-mycompletion)
-       (setq iswitchb-use-mycompletion t)
-       (make-local-hook 'pre-command-hook)
-       (add-hook 'pre-command-hook
-                 'iswitchb-pre-command
-                 nil t)
-       (make-local-hook 'post-command-hook)
-       (add-hook 'post-command-hook
-                 'iswitchb-post-command
-                 nil t)
-       
-       (run-hooks 'iswitchb-minibuffer-setup-hook))))
+  (when (iswitchb-entryfn-p)
+    (set (make-local-variable 'iswitchb-use-mycompletion) t)
+    (add-hook 'pre-command-hook 'iswitchb-pre-command nil t)
+    (add-hook 'post-command-hook 'iswitchb-post-command nil t)
+    (run-hooks 'iswitchb-minibuffer-setup-hook)))
 
 (defun iswitchb-pre-command ()
   "Run before command in `iswitchb-buffer'."
@@ -1291,14 +1272,7 @@ Copied from `icomplete-tidy'."
 
 (defun iswitchb-entryfn-p ()
   "Return non-nil if we are using `iswitchb-buffer'."
-  ;; Testing if `iswitchb-prepost-hooks' is bound does not work when
-  ;; we're invoking a recursive mini-buffer from an Iswitchb buffer.
-  ;; In this case, `iswitchb-prepost-hooks' is bound in the second
-  ;; mini-buffer, although it's not an Iswitchb buffer.
-  (memq this-command
-       '(iswitchb-buffer iswitchb-buffer-other-frame
-                         iswitchb-display-buffer
-                         iswitchb-buffer-other-window)))
+  (eq iswitchb-minibuf-depth (minibuffer-depth)))
 
 (defun iswitchb-summaries-to-end ()
   "Move the summaries to the end of the list.
@@ -1322,27 +1296,15 @@ See the variable `iswitchb-case' for details."
        (isearch-no-upper-case-p iswitchb-text t))))
 
 ;;;###autoload
-(defun iswitchb-mode (&optional arg)
+(define-minor-mode iswitchb-mode
   "Toggle Iswitchb global minor mode.
 With arg, turn Iswitchb mode on if and only iff ARG is positive.
 This mode enables switching between buffers using substrings.  See
 `iswitchb' for details."
-  (interactive "P")
-  (setq iswitchb-mode
-       (if arg
-           (> (prefix-numeric-value arg) 0)
-         (not iswitchb-mode)))
+  nil nil iswitchb-global-map :global t :group 'iswitchb
   (if iswitchb-mode
       (add-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)
-    (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup))
-  (run-hooks 'iswitchb-mode-hook)
-  (if (interactive-p)
-      (message "Iswitchb mode %sabled"
-              (if iswitchb-mode "en" "dis"))))
-
-(unless (assq 'iswitchb-mode minor-mode-map-alist)
-  (push (cons 'iswitchb-mode iswitchb-global-map)
-       minor-mode-map-alist))
+    (remove-hook 'minibuffer-setup-hook 'iswitchb-minibuffer-setup)))
 
 (provide 'iswitchb)