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

  1. Path: sparky!uunet!auspex-gw!wally
  2. From: wally@Auspex.COM (Wally Bass)
  3. Newsgroups: comp.emacs
  4. Subject: Re: anyone got a way to cycle through buffers with a keystroke?
  5. Keywords: buffers
  6. Message-ID: <15385@auspex-gw.auspex.com>
  7. Date: 7 Nov 92 01:46:39 GMT
  8. References: <EJH.92Nov4132905@khonshu.colorado.edu>
  9. Sender: news@auspex-gw.auspex.com
  10. Organization: Auspex Systems, Santa Clara
  11. Lines: 116
  12. Nntp-Posting-Host: auspex.auspex.com
  13.  
  14. In article <EJH.92Nov4132905@khonshu.colorado.edu> ejh@khonshu.colorado.edu (Edward J. Hartnett) writes:
  15. >
  16. >Hi. I often have maybe 5 or 10 programs and other files open at once,
  17. >and switch back and forth more or less randomly. It's very annoying
  18. >that I have to tyep out the buffer name I want each time (even with
  19. >tab completion). Does anyone out there have any elisp code that would
  20. >allow me to cycle through the buffers with a keystroke? So I'd hit a
  21. >key and the buffer would switch to the next one, then again and it
  22. >would go to the next, etc. Then I could just pop pop pop on the key
  23. >until the buffer I wanted came up.
  24. >--
  25. >Edward Hartnett            ejh@khonshu.colorado.edu
  26.  
  27. The following code cycles thru the ring of buffers with a
  28. single key, but also does a little bit more. Suppose you bind
  29. the function 'go-buffer-ring' to your 'foo' key, and 'buf-xcld-key'
  30. to your 'bar' key. Then
  31.  
  32.   pressing 'bar' places (or removes) (toggles) the current buffer name
  33.         in a list of names which will be excluded when cycling thru
  34.         buffers with the 'foo' key. A message is also displayed which
  35.         indicates the new state of the current buffer (e.g., 'included'
  36.         or 'excluded')
  37.   (prefix-key (i.e., C-u), 'bar') clears the exclude list
  38.   repeatedly pressing 'foo' cycles thru buffers in the buffer list,
  39.         but skips any buffers which are on the exclude list. It also
  40.         displays a message indicating the complete file name or the
  41.         directory name (for dired buffers) of the new buffer, or displays
  42.         'none' if all buffers are currently excluded.
  43.   repeatedly pressing (prefix-key, 'foo') cycles thru buffers,
  44.         but ONLY displays buffers which ARE on the exclude list. It
  45.         also displays pathnames or 'none' as appropriate.
  46.  
  47. Note that, when you alternately press ('foo') and (prefix-key,'foo'),
  48. you will toggle between the 'first' file in each category (excluded
  49. or included), but you will not advance thru either set. This mode
  50. is also useful for toggling between exactly two files.
  51.  
  52. (This is inter-twined with other stuff in my .emacs file. I think/hope
  53. I extracted a complete and coherent chunk.)
  54.  
  55. -------------------------------
  56. (defvar buffer-exclude-list nil
  57.   "list of buffers not to be included in go-buffer-ring")
  58.  
  59. (defun identify ()
  60.   (cond
  61.     ((eq major-mode 'dired-mode) (message "dir: %s" default-directory))
  62.     (buffer-file-name (message "file: %s" buffer-file-name))
  63.     (t (message "")) ) )
  64.  
  65. (defun chk-buf-xcld-lst (arg purge)
  66.   "Checks if buffer is on exclude list. With non-nil 2nd arg, also removes
  67. the buffer name from the list if found"
  68.   (let ((lp buffer-exclude-list)
  69.         (prev nil)
  70.     (ans nil) )  
  71.      (while (and lp (not ans))
  72.        (if (equal arg (car lp))
  73.      (setq ans t)
  74.          (setq prev lp)
  75.      (setq lp (cdr lp)) ) )
  76.      (if (and purge ans)
  77.        (if prev
  78.          (setcdr prev (if lp (cdr lp) nil))
  79.          (setq buffer-exclude-list (if lp (cdr lp) nil)) ) ) 
  80.      ans ) )
  81.        
  82. (defun go-buffer-ring (arg)
  83.   "Cycles around a ring of buffers"
  84.   (interactive "P")
  85.   (let ((list (buffer-list))
  86.         (sw t)
  87.     (msg "none")
  88.     x xc )
  89.     (while
  90.       (and list
  91.     (or
  92.       (equal (substring (buffer-name (car list)) 0 1) " ")
  93.       (prog1
  94.         sw
  95.         (setq x (chk-buf-xcld-lst (buffer-name (car list)) nil))
  96.         (if sw (setq xc x))
  97.         (setq sw nil)
  98.             (setq x (if arg (not x) x))
  99.         (if (not x) (setq msg nil)) )
  100.       x ) )
  101.       (setq list (cdr list)) )
  102.     (cond
  103.       (list
  104.     (if (if (chk-buf-xcld-lst (buffer-name (car list)) nil) xc (not xc)) 
  105.       (bury-buffer (current-buffer)) )
  106.     (switch-to-buffer (car list))
  107.     (set-buffer (window-buffer (selected-window))) ) )
  108.     (if msg (message msg) (identify)) ) )
  109.  
  110. (defun buf-xcld-key (arg)
  111.   "Adds or removes a buffer from the buffer-ring exclude list.
  112. With arg, clears the exclude list"
  113.   (interactive "P")
  114.   (let ((bufname nil)
  115.     (msg "exclude list cleared") )
  116.     (if arg (setq buffer-exclude-list nil)
  117.       (setq msg "now excluded")
  118.       (setq bufname (buffer-list))
  119.       (while
  120.     (and bufname (equal (substring (buffer-name (car bufname)) 0 1) " "))
  121.     (setq bufname (cdr bufname)) )
  122.       (setq bufname
  123.     (if bufname (buffer-name (car bufname)) "") )
  124.       (if (chk-buf-xcld-lst bufname t)
  125.     (setq msg "now included")
  126.     (setq buffer-exclude-list
  127.       (cons bufname buffer-exclude-list) ) ) )
  128.     (message msg) ) )
  129. ---------------
  130.