home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / emacs / 3488 < prev    next >
Encoding:
Text File  |  1992-11-08  |  4.7 KB  |  132 lines

  1. Newsgroups: comp.emacs
  2. Path: sparky!uunet!ftpbox!mothost!merlin.dev.cdx.mot.com!fendahl.dev.cdx.mot.com!mcook
  3. From: mcook@fendahl.dev.cdx.mot.com (Michael Cook)
  4. Subject: Re: anyone got a way to cycle through buffers with a keystroke?
  5. Message-ID: <mcook.721184271@fendahl.dev.cdx.mot.com>
  6. Sender: news@merlin.dev.cdx.mot.com (USENET News System)
  7. Nntp-Posting-Host: fendahl.dev.cdx.mot.com
  8. Organization: Motorola Codex, Canton, Massachusetts
  9. References: <EJH.92Nov4132905@khonshu.colorado.edu>
  10. Date: Sun, 8 Nov 1992 00:57:51 GMT
  11. Lines: 119
  12.  
  13. ejh@khonshu.colorado.edu (Edward J. Hartnett) writes:
  14.  
  15. >Hi. I often have maybe 5 or 10 programs and other files open at once,
  16. >and switch back and forth more or less randomly. It's very annoying
  17. >that I have to tyep out the buffer name I want each time (even with
  18. >tab completion). Does anyone out there have any elisp code that would
  19. >allow me to cycle through the buffers with a keystroke? So I'd hit a
  20. >key and the buffer would switch to the next one, then again and it
  21. >would go to the next, etc. Then I could just pop pop pop on the key
  22. >until the buffer I wanted came up.
  23.  
  24. You've gotten quite a few follow-ups, so here's another with a slightly
  25. different approach.  iswitch-to-buffer is an isearch-like version of
  26. switch-to-buffer.  You need type only the first few characters of the buffer
  27. name -- enough to uniquely identify the buffer.  Once you've typed enough, it
  28. tells you by saying "[Matched]".  If there are multiple buffers whose name is
  29. matched by what you've typed, and their names have a common part that you
  30. haven't typed yet, that common part is shown in parens; if you hit TAB, it
  31. pretends you typed that whole common part.  Press RET to finish.
  32.  
  33. I find that I use iswitch-to-buffer when I have a lot of buffers and I want to
  34. switch to one that I haven't visited in quite a while.  I know that that
  35. buffer will be quite a ways down the buffer list, so I don't want to have to
  36. walk down a long list.
  37.  
  38. Michael.
  39.  
  40. #!/bin/sh
  41. # to extract, remove the header and type "sh filename"
  42. if `test ! -s ./iswitch.el`
  43. then
  44. echo "writing ./iswitch.el"
  45. cat > ./iswitch.el << '\End\Of\Shar\'
  46. (defun iswitch-to-buffer ()
  47.   "An isearch-like version of switch-to-buffer."
  48.   (interactive)
  49.   (let ((buf (save-window-excursion
  50.            (catch 'iswitch-done
  51.          (let ((bufs (apply 'append
  52.                     (mapcar '(lambda (buf)
  53.                            (if (eq buf (current-buffer))
  54.                            nil
  55.                          (list (cons (buffer-name buf) buf))))
  56.                         (buffer-list))))
  57.                (name '(""))
  58.                (completion-ignore-case t))
  59.            (while t
  60.              (if (or (>= unread-command-char 0)
  61.                  (input-pending-p))
  62.              nil
  63.                (let ((comps (all-completions (car name) bufs)))
  64.              (message "Switch to buffer: %s%s"
  65.                   (car name)
  66.                   (cond ((null comps)
  67.                      " [No matches]")
  68.                     ((null (cdr comps)) ;one match
  69.                      (concat
  70.                       (and (> (length (car comps))
  71.                           (length (car name)))
  72.                            (concat "(" (substring (car comps) (length (car name))) ")"))
  73.                       " [Matched]"))
  74.                     (t ;multiple matches
  75.                      (let ((most (try-completion (car name) bufs)))
  76.                        (if (<= (length most) (length (car name)))
  77.                            ""
  78.                          (concat "(" (substring most (length (car name))) ")"))))))))
  79.              (let ((char (read-char)))
  80.                (cond ((eq char ?\177)
  81.                   (if (cdr name)
  82.                   (setq name (cdr name))))
  83.                  ((eq char ?\t)
  84.                   (let ((most (try-completion (car name) bufs)))
  85.                 (if (> (length most) (length (car name)))
  86.                     (setq name (cons most name))
  87.                   (iswitch-show-completions (car name) bufs))))
  88.                  ((eq char ??)
  89.                   (iswitch-show-completions (car name) bufs))
  90.                  ((or (eq char ?\C-m)
  91.                   (eq char ?\C-j))
  92.                   (let ((most (try-completion (car name) bufs)))
  93.                 (let ((buf (cond ((eq most t)
  94.                           (get-buffer (car name)))
  95.                          (most
  96.                           (get-buffer most)))))
  97.                   (if (not buf)
  98.                       (let ((match (car (all-completions (car name) bufs))))
  99.                     (setq buf (and match (get-buffer match)))))
  100.                   (and buf
  101.                        (throw 'iswitch-done buf))
  102.                   (beep))))
  103.                  ((and (>= char ? )
  104.                    (<= char ?~))
  105.                   (setq name (cons (concat (car name)
  106.                                (char-to-string char))
  107.                            name)))
  108.                  (t
  109.                   (setq unread-command-char char)
  110.                   (throw 'iswitch-done nil))))))))))
  111.     (and buf
  112.      (switch-to-buffer buf))))
  113.  
  114. (defun iswitch-show-completions (name bufs)
  115.   (let ((comps (all-completions name bufs)))
  116.     (cond ((null comps)
  117.        (message "No matches.")
  118.        (sit-for 2))
  119.       (t
  120.        (with-output-to-temp-buffer "*Help*"
  121.          (display-completion-list comps))))))
  122. \End\Of\Shar\
  123. else
  124.   echo "will not over write ./iswitch.el"
  125. fi
  126. if [ `wc -c ./iswitch.el | awk '{printf $1}'` -ne 2449 ]
  127. then
  128. echo `wc -c ./iswitch.el | awk '{print "Got " $1 ", Expected " 2449}'`
  129. fi
  130. echo "Finished archive 1 of 1"
  131. exit
  132.