home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / buf-sel.el < prev    next >
Encoding:
Text File  |  1993-06-15  |  6.7 KB  |  178 lines

  1. ;; LCD Archive Entry:
  2. ;; buf-sel|Tomn Horsley|thorsley@ssd.csd.harris.com|
  3. ;; Interactive buffer-list in the minibuffer.|
  4. ;; 92-03-25||~/misc/buf-sel.el.Z|
  5. ;;
  6. ;; =====================================================================
  7. ;;     usenet: thorsley@ssd.csd.harris.com  USMail: Tom Horsley
  8. ;; compuserve: 76505,364                         511 Kingbird Circle
  9. ;;      genie: T.HORSLEY                         Delray Beach, FL  33444
  10. ;; ======================== Aging: Just say no! ========================
  11. ;;
  12. ;; This file provides an interactive buffer-list capability.
  13. ;; When the function select-buffer is invoked, the minibuffer
  14. ;; prompts you for another buffer to select.  The default is the second
  15. ;; buffer on the buffer-list.  Also, all the keys that are normally
  16. ;; bound to next-line and previous-line are bound to functions that
  17. ;; navigate through the buffer list.  Any keys bound to kill-buffer
  18. ;; are rebound to a function that will kill the buffer currently
  19. ;; named in the minibuffer, then move to the next buffer on the list.
  20. ;; This is a faster means of selecting another buffer than buffer-menu
  21. ;; is, but with most of the power.
  22. ;; 
  23. ;; Bob Weiner, Motorola, Inc., 4/5/89
  24. ;;   Added 'select-buffer-other-window' command bound to {C-x4b}
  25. ;;   usually.
  26. ;; 
  27. ;; Bob Weiner, Motorola, Inc., 10/3/91
  28. ;;   Eliminated inefficient recursion for computing buffer list.
  29. ;;   This eliminated error of passing max-lisp-eval-depth when working
  30. ;;     with many buffers.
  31. ;;   Added completion to 'select-buffer' so it works like standard
  32. ;;     'switch-to-buffer' function.
  33. ;;
  34. ;; We have gotten to where we use this technique for several of the
  35. ;; packages we have written where something prompts for input, each
  36. ;; command keeps its own history list so you can quickly cycle through
  37. ;; the previous input to just that command.
  38. ;; 
  39. ;; It is very handy to rebind the keys where next line is, so you can
  40. ;; continue to use any cursor keys.
  41. ;;
  42. ;; (autoload 'select-buffer "buff-sel" nil t)
  43. ;; (global-set-key "\C-xb" 'select-buffer)
  44. ;; (global-set-key "\C-x4b" 'select-buffer-other-window)
  45.  
  46. (defvar buffer-select-list-index 0 "Index into buffer-list")
  47.  
  48. (defvar buffer-select-local-list nil "Local copy of buffer-list")
  49.  
  50. (defvar buffer-select-minibuffer-map
  51.   (copy-keymap minibuffer-local-must-match-map)
  52.   "This is a copy of the minibuffer completion keymap with all the keys that
  53. were bound to next-line now bound to buffer-select-next and all the keys
  54. that were bound to previous-line now bound to buffer-select-prev.")
  55.  
  56. (mapcar
  57.  (function 
  58.   (lambda (keyseq)
  59.     (define-key buffer-select-minibuffer-map keyseq 'buffer-select-prev)))
  60.  (where-is-internal 'previous-line nil nil))
  61.  
  62. (mapcar
  63.  (function 
  64.   (lambda (keyseq)
  65.     (define-key buffer-select-minibuffer-map keyseq 'buffer-select-next)))
  66.    (where-is-internal 'next-line nil nil))
  67.  
  68. (mapcar
  69.  (function 
  70.   (lambda (keyseq)
  71.     (define-key buffer-select-minibuffer-map keyseq 'buffer-select-kill-buf)))
  72.    (where-is-internal 'kill-buffer nil nil))
  73.  
  74. (defun make-buffer-list (buffer-list)
  75.   "Returns names from BUFFER-LIST excluding those beginning with a space."
  76.   (delq nil (mapcar '(lambda (b)
  77.                (if (= (aref (buffer-name b) 0) ? ) nil b))
  78.             buffer-list)))
  79.  
  80. (defun select-buffer (&optional other-window)
  81.   "Interactively select or kill buffer using the minibuffer.
  82. Optional argument OTHER-WINDOW non-nil means display buffer in another window.
  83. The default buffer is the second one in the buffer-list. Other buffers can
  84. selected either explicitly, or by using buffer-select-next and
  85. buffer-select-prev.  Keys normally bound to next-line are bound to
  86. buffer-select-next, those normally bound to previous-line are bound to
  87. buffer-select-prev, and those normally bound to kill-buffer are bound to
  88. buffer-select-kill-buf."
  89.    (interactive)
  90.    (let ((save-minibuffer-map minibuffer-local-must-match-map)
  91.          inpt)
  92.       (setq inpt
  93.             (unwind-protect
  94.                (progn
  95.          (setq minibuffer-local-must-match-map
  96.                buffer-select-minibuffer-map
  97.                buffer-select-list-index 1
  98.                buffer-select-local-list
  99.                (make-buffer-list (buffer-list)))
  100.                   (completing-read (concat "Switch to buffer"
  101.                       (if other-window " in other window")
  102.                       ": ")
  103.                    (mapcar '(lambda (buf)
  104.                           (list (buffer-name buf)))
  105.                        buffer-select-local-list)
  106.                    nil t
  107.                    (buffer-name
  108.                     (car (cdr buffer-select-local-list))))
  109.           )
  110.           (setq minibuffer-local-must-match-map save-minibuffer-map)
  111.           ))
  112.       (if other-window
  113.       (switch-to-buffer-other-window inpt)
  114.     (switch-to-buffer inpt))
  115.       ))
  116.  
  117. (defun select-buffer-other-window ()
  118.   "See documentation for 'select-buffer'."
  119.   (interactive)
  120.   (select-buffer t))
  121.  
  122. (defun buffer-select-next ()
  123. "Move to the next buffer on the buffer-list."
  124.    (interactive)
  125.    (erase-buffer)
  126.    (setq buffer-select-list-index (1+ buffer-select-list-index))
  127.    (if (>= buffer-select-list-index (length buffer-select-local-list))
  128.        (setq buffer-select-list-index 0)
  129.    )
  130.    (insert (buffer-name (nth buffer-select-list-index buffer-select-local-list)))
  131. )
  132.  
  133. (defun buffer-select-prev ()
  134. "Move to the previous buffer on the buffer-list."
  135.    (interactive)
  136.    (erase-buffer)
  137.    (setq buffer-select-list-index (1- buffer-select-list-index))
  138.    (if (< buffer-select-list-index 0)
  139.        (setq buffer-select-list-index (1- (length buffer-select-local-list)))
  140.    )
  141.    (insert (buffer-name
  142.               (nth buffer-select-list-index buffer-select-local-list)))
  143. )
  144.  
  145. (defun buffer-select-kill-buf ()
  146. "Kill the buffer currently appearing in the minibuffer, then move to
  147. the next buffer on the buffer-list."
  148.    (interactive)
  149.    (let
  150.       (
  151.          (mbuf (current-buffer))        ;; Save the minibuffer because
  152.                                         ;; kill-buffer selects a buffer
  153.          (kbuf (nth buffer-select-list-index buffer-select-local-list))
  154.       )
  155.       (message "Killing buffer %s." (buffer-name kbuf))
  156.       (kill-buffer kbuf)
  157.       (set-buffer mbuf)
  158.    )
  159.    ;; Rebuild the buffer list, so that the killed buffer doesn't appear
  160.    ;; in it.  Under certain circumstances, the buffer might not have
  161.    ;; gone away, such as killing "*scratch*" when it is the last buffer.
  162.    
  163.    (setq buffer-select-local-list (make-buffer-list (buffer-list)))
  164.    
  165.    ;; Fix buffer-select-list-index, in case it went off the end of
  166.    ;; the list (in either direction, just to be absolutely safe).
  167.  
  168.    (if (< buffer-select-list-index 0)
  169.        (setq buffer-select-list-index (1- (length buffer-select-local-list)))
  170.    )
  171.    (if (>= buffer-select-list-index (length buffer-select-local-list))
  172.        (setq buffer-select-list-index 0)
  173.    )
  174.    (erase-buffer)
  175.    (insert (buffer-name
  176.               (nth buffer-select-list-index buffer-select-local-list)))
  177. )
  178.