Skip to content

Instantly share code, notes, and snippets.

@seanirby
Last active March 8, 2016 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanirby/5a582e48486fcd5669f9 to your computer and use it in GitHub Desktop.
Save seanirby/5a582e48486fcd5669f9 to your computer and use it in GitHub Desktop.
;;;; Hydra for quickly switching among the 8 most recent buffers
;;;; by using the home row keys.
;; This is the command that toggles the hydra.
;; Change the keybinding if you like
(global-set-key (kbd "C-c j") 'hydra-bswitch/body)
(defvar bswitch-map (list 1 "a"
2 "s"
3 "d"
4 "f"
5 "j"
6 "k"
7 "l"
8 ";"))
(defvar bswitch-offset 15)
(defvar bswitch-key-buffer-spacing 5)
(defun bswitch-repeat (number &optional str)
(if (< number 1)
str
(bswitch-repeat (1- number) (concat " " str))))
(defun bswitch-get-beginning ()
(bswitch-repeat (- (/ (window-body-width) 2) bswitch-offset)))
(defun bswitch-get-end (beg mid)
(bswitch-repeat (- (window-body-width)
(+ (length mid)
(length beg)))))
(defun bswitch-get-header-display-line ()
(let* ((beg (bswitch-get-beginning))
(mid (concat "Key"
(bswitch-repeat
(+ -2 bswitch-key-buffer-spacing))
"Buffer"))
(end (bswitch-get-end beg mid)))
(concat beg mid end)))
(defun bswitch-get-buffer-display-line (index)
(let* ((blist (buffer-list)))
(if (< index (length blist))
(let* ((beg (bswitch-get-beginning))
(mid (concat (plist-get bswitch-map index)
(bswitch-repeat bswitch-key-buffer-spacing)
(buffer-name (nth index (buffer-list)))))
(end (bswitch-get-end beg mid)))
(concat beg mid end))
(let* ((beg (bswitch-get-beginning))
(mid (format "No buffer at index %s yet" index))
(end (bswitch-get-end beg mid)))
(concat beg mid end)))))
(defun bswitch-switch (index)
(if (< index (length (buffer-list)))
(switch-to-buffer (nth index (buffer-list)))
(message "Cannot switch since there is no buffer at index %s yet" index)))
(defhydra hydra-bswitch (:hint nil :exit t)
"
%(bswitch-get-header-display-line)
%(bswitch-get-buffer-display-line 1)
%(bswitch-get-buffer-display-line 2)
%(bswitch-get-buffer-display-line 3)
%(bswitch-get-buffer-display-line 4)
%(bswitch-get-buffer-display-line 5)
%(bswitch-get-buffer-display-line 6)
%(bswitch-get-buffer-display-line 7)
%(bswitch-get-buffer-display-line 8)
"
("a" (bswitch-switch 1))
("s" (bswitch-switch 2))
("d" (bswitch-switch 3))
("f" (bswitch-switch 4))
("j" (bswitch-switch 5))
("k" (bswitch-switch 6))
("l" (bswitch-switch 7))
(";" (bswitch-switch 8))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment