Wednesday, August 4, 2010

ignoring emacs auto buffers in (next/previous)-buffer

I am about 2 yr old emacs user now. I've been doing basic stuff with emacs for long and now trying to transition into emacs power user after getting inspiration from screencasts like this one. More such vids can be found at emacs wiki.

Anyway, The traditional commands for going to next and previous buffers in emacs are "C-x <left/right arrow>". I read a pro tip somewhere that whenever you use next/prev buffer commands, you almost never want to go to one of emacs auto buffers such as *scratch*, *Messages* etc and that unnecessarily slows you down. It sounded so true to me and I got the itch to write elisp for it that ignores those buffers and here it is(I'm sure it could be done better, but then I'm a noob :) )
(defun emacs-buffer-p (name)
(string-match-p "\\*.*\\*" name))

(defun next-non-emacs-buffer (&optional original)
"Similar to next-buffer, but ignores emacs buffer such as *scratch*, *messages* etc."
(interactive)
(let ((tmp-orig (or original (buffer-name))))
(next-buffer)
(if (and
(not (eq (buffer-name) tmp-orig))
(emacs-buffer-p (buffer-name)))
(next-non-emacs-buffer tmp-orig))))

(defun previous-non-emacs-buffer (&optional original)
"Similar to previous-buffer, but ignores emacs buffer such as *scratch*, *messages* etc."
(interactive)
(let ((tmp-orig (or original (buffer-name))))
(previous-buffer)
(if (and
(not (eq (buffer-name) tmp-orig))
(emacs-buffer-p (buffer-name)))
(previous-non-emacs-buffer tmp-orig))))

and re-mapped "C-b", "M-b"(I never used their default keybindings anyway) to them
(global-set-key (kbd "C-b") 'next-non-emacs-buffer)
(global-set-key (kbd "M-b") 'previous-non-emacs-buffer)

Now if, occasionally, I want to goto emacs auto buffers then I can still use "C-x <left/right arrow>".

No comments:

Post a Comment