2021-02-21 02:59:11 +00:00
|
|
|
;; -*- lexical-binding: t; -*-
|
|
|
|
|
2021-02-20 11:40:03 +00:00
|
|
|
;; Buffer-related configuration
|
|
|
|
(defun kill-other-buffers ()
|
|
|
|
"Kill all other buffers."
|
|
|
|
(interactive)
|
|
|
|
(mapc 'kill-buffer (delq (current-buffer) (buffer-list))))
|
|
|
|
|
2021-02-23 16:07:23 +00:00
|
|
|
(defun switch-to-previous-buffer ()
|
|
|
|
"Switch to previously open buffer.
|
|
|
|
Repeated invocations toggle between the two most recently open buffers."
|
|
|
|
(interactive)
|
|
|
|
(switch-to-buffer (other-buffer (current-buffer) 1)))
|
|
|
|
|
2021-05-12 15:02:13 +00:00
|
|
|
(defun get-buffers-matching-mode (mode)
|
|
|
|
"Returns a list of buffers where their major-mode is equal to MODE"
|
|
|
|
(let ((buffer-mode-matches '()))
|
|
|
|
(dolist (buf (buffer-list))
|
|
|
|
(with-current-buffer buf
|
|
|
|
(if (eq mode major-mode)
|
|
|
|
(add-to-list 'buffer-mode-matches buf))))
|
|
|
|
buffer-mode-matches))
|
|
|
|
|
|
|
|
(defun get-buffer-modes ()
|
|
|
|
"Returns a list of all major modes in currenctly open buffers."
|
|
|
|
(delete-dups
|
|
|
|
(cl-loop for buf in (buffer-list)
|
|
|
|
collect (with-current-buffer buf major-mode))))
|
|
|
|
|
|
|
|
(defun multi-occur-in-mode (mode regexp)
|
|
|
|
"Show all lines matching REGEXP in buffers with this major mode."
|
|
|
|
(interactive (list (completing-read "List lines from buffers with mode: "
|
|
|
|
(get-buffer-modes)
|
|
|
|
nil
|
|
|
|
'confirm
|
|
|
|
nil
|
|
|
|
nil
|
|
|
|
major-mode)
|
|
|
|
(read-string "List lines matching regexp: ")))
|
|
|
|
(multi-occur (get-buffers-matching-mode mode) regexp))
|
|
|
|
|
|
|
|
(defun occur-all (regexp)
|
|
|
|
"Show all lines matching REGEXP in all open buffers."
|
|
|
|
(interactive "MList lines matching regexp: ")
|
|
|
|
(multi-occur-in-matching-buffers ".*" regexp t))
|
2021-02-23 16:07:23 +00:00
|
|
|
|
2021-02-20 11:40:03 +00:00
|
|
|
(leader-def-key
|
2021-02-23 16:07:23 +00:00
|
|
|
"TAB" #'switch-to-previous-buffer
|
2021-02-20 11:40:03 +00:00
|
|
|
"b" '(nil :which-key "buffer")
|
|
|
|
"bb" #'switch-to-buffer
|
|
|
|
"bd" #'kill-buffer
|
|
|
|
"bm" #'kill-other-buffers
|
2021-03-19 16:40:01 +00:00
|
|
|
"br" #'rename-buffer
|
2021-05-12 15:02:13 +00:00
|
|
|
"bg" #'revert-buffer
|
|
|
|
"bo" #'occur-all)
|
2021-02-20 11:40:03 +00:00
|
|
|
|
2021-02-26 15:28:50 +00:00
|
|
|
;; A library that provides a hook called on switching buffers
|
|
|
|
(use-package switch-buffer-functions
|
|
|
|
:demand t)
|
|
|
|
|
2021-02-20 11:40:03 +00:00
|
|
|
(provide 'init-buffers)
|