Skip to content

Instantly share code, notes, and snippets.

@jqtruong
Last active December 19, 2015 06:19
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 jqtruong/5910656 to your computer and use it in GitHub Desktop.
Save jqtruong/5910656 to your computer and use it in GitHub Desktop.
Use set-temporary-overlay-map to create single keybindings and by learning to let go of control...in emacs. Code learned from shell-switcher.el in which case C-' starts a shell then repeatedly hitting ' cycles to the next shell. In the example below, "C-, f" goes to the next window, then either b or f to move back and forth. The other example, "…
;;;;;;;;;;;;
;; macros ;;
;;;;;;;;;;;;
(defmacro repeater-map (repeat-key fun)
"For one-key maps to repeatly called `fun'."
`(set-temporary-overlay-map
(let ((map (make-sparse-keymap)))
(define-key map (vector ,repeat-key)
,fun)
map) t))
(defmacro repeater-map-more (keymaps)
"For complex maps in which multiple repeatable keys are attached to
functions."
`(set-temporary-overlay-map
(let ((map (make-sparse-keymap)))
(loop
for (repeat-key fun) in ,keymaps
do (define-key map (vector repeat-key)
`(lambda () (interactive)
(if (listp ',fun)
(apply (car ,fun) (cdr ,fun))
(,fun)))))
map) t))
;;;;;;;;;;;;;
;; helpers ;;
;;;;;;;;;;;;;
(defun jqt/continue (fun)
"Helper method to set the repeat-key before calling `repeater-map'."
(let ((repeat-key (event-basic-type last-input-event)))
(repeater-map repeat-key fun)))
(defun jqt/continue-more (keymaps)
"Helper method for not much at the moment..."
(repeater-map-more keymaps))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; window/buffer handlers ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun jqt/continue-rotating-buffers-in-windows ()
"Calls `jqt/rotate-buffers-in-windows' and sets up the temporary
map."
(interactive)
(jqt/rotate-buffers-in-windows)
(jqt/continue 'jqt/rotate-buffers-in-windows))
(defun jqt/rotate-buffers-in-windows ()
"Move current buffer to next window and so on such that current
window will get the previous one's buffer."
(interactive)
(walk-windows
(lambda (window)
(set-window-buffer window (other-buffer nil))))
(switch-to-buffer nil))
(defun jqt/continue-switching-windows (&optional counter-clockwise-p)
"Calls `jqt/switch-windows' and sets up the temporary map."
(interactive "P")
(jqt/switch-windows counter-clockwise-p)
(jqt/continue-more
'((?f jqt/switch-windows)
(?b '(jqt/switch-windows t)))))
(defun jqt/switch-windows (&optional counter-clockwise)
"Convenience keybinding to switch windows."
(interactive "P")
(if counter-clockwise
(other-window -1)
(other-window 1)))
;;;;;;;;;;;;;;;;;
;; keybindings ;;
;;;;;;;;;;;;;;;;;
(global-set-key (kbd "C-, f") 'jqt/continue-switching-windows)
(global-set-key (kbd "C-, b") (lambda () (interactive)
(jqt/continue-switching-windows 1)))
(global-set-key (kbd "C-, s") 'jqt/continue-rotating-buffers-in-windows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment