Skip to content

Instantly share code, notes, and snippets.

@caiorss
Created February 17, 2017 19:59
Show Gist options
  • Save caiorss/ab7bce6587789abdf86dd6d928e3ee60 to your computer and use it in GitHub Desktop.
Save caiorss/ab7bce6587789abdf86dd6d928e3ee60 to your computer and use it in GitHub Desktop.
Emacs/ Elisp - Switch between buffer types. Example switch between only c++ *.cpp (c++-mode), files, c-mode *.c, c-mode *.h and so on.
;; Question: Is there a package/lisp code that lets me cycle through filetype buffers?
;; Reddit tpic: https://www.reddit.com/r/emacs/comments/5tjgsb/is_there_a_packagelisp_code_that_lets_me_cycle/
;;
;;
(require 'helm) ;;
(require 'cl) ;;
(defun switch-buffer-type (buffer-major-mode file-extension)
(let ((items (mapcar
(lambda (buf) (cons (buffer-name buf) buf))
(remove-if-not (lambda (buf)
(and ;; if buffer-major-mode is nil - select buffer by file extension
(if buffer-major-mode
(equal buffer-major-mode (buffer-local-value 'major-mode buf))
t
)
;; if file-extension is null - select buffer by major-mode
(if file-extension
(string-suffix-p file-extension (buffer-file-name buf))
t
)))
(buffer-list)))))
(helm
:prompt "Buffer switch: "
:sources `((
(name . "Dir: ")
(candidates . ,items)
(action . switch-to-buffer)
)))))
(defun switch-cpp-files ()
(interactive)
(switch-buffer-type 'c++-mode ".cpp"))
(defun switch-h-files ()
(interactive)
(switch-buffer-type 'c-mode ".h"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment