[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

find buffer by regexp, then iterate (very cool)

From: Emanuel Berg
Subject: find buffer by regexp, then iterate (very cool)
Date: Thu, 30 Jun 2016 15:45:52 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

(require 'cl-lib)

(defun buffer-names ()
  "Get a list of all `buffer-name's."
  (mapcar #'buffer-name (buffer-list)) )

(defun switch-to-buffer-regexp (buffer-regexp &optional ori-buffer)
  (let*((original-buffer (or ori-buffer (current-buffer)))
        (hit (cl-find-if
              (lambda (b) (string-match buffer-regexp b))
              (buffer-names) ))
        (hit-buffer  (when hit        (get-buffer hit)))
        (same-buffer (when hit-buffer (eq original-buffer hit-buffer))) )
    (if hit-buffer
        (if same-buffer
            (if ori-buffer
                  (message "(This buffer is the only hit!)")
                  (switch-to-buffer ori-buffer) )
                (switch-to-buffer-regexp buffer-regexp original-buffer) ))
          (switch-to-buffer hit-buffer) )
      (error "No hit: %s" buffer-regexp) )))

;; Try these -

;; (global-set-key "\C-ca" (lambda () (interactive) (switch-to-buffer-regexp 
;; (global-set-key "\C-ch" (lambda () (interactive) (switch-to-buffer-regexp 
;; (global-set-key "\C-cc" (lambda () (interactive) (switch-to-buffer-regexp 

;; - and this, the most interesting case:
;;   create four posts, go to a post, then hit
;;   again to iterate!

;; (require 'gnus)
;; (dotimes (i 4) (gnus-post-news 'post "")) ;; create 4 posts
;; (global-set-key "\C-cu" (lambda () (interactive) (switch-to-buffer-regexp 
"\*unsent posting\*.*")))

(provide 'switch-to-buffer-regexp)

underground experts united ....
Emacs Gnus Blogomatic .........
                   - so far: 55 Blogomatic articles -                   

reply via email to

[Prev in Thread] Current Thread [Next in Thread]