Public
Authored by aerique

aerique's EXWM Functions

disclaimer: I'm using evil-mode and these functions expect that

Moving Buffers Around

The functions below are to quickly move a buffer to another window because the way I usually managed buffers (helm-buffers-list bound to C-<return>) was too slow when using Emacs as a window manager.

  • s-<up>: evil-window-up
  • s-<down>: evil-window-down
  • s-<right>: evil-window-right
  • s-<left>: evil-window-left
  • s-S-<up>: my-exwm-move-buffer-up
  • s-S-<down>: my-exwm-move-buffer-down
  • s-S-<right>: my-exwm-move-buffer-right
  • s-S-<left>: my-exwm-move-buffer-left
  • s-C-<up>: enlarge-window
  • s-C-<down>: shrink-window
  • s-C-<right>: enlarge-window-horizontally
  • s-C-<left>: shrink-window-horizontally
(defun my-exwm-move-buffer-right ()
  (interactive)
  (let ((buffer (current-buffer)))
    (evil-window-right 1)
    (switch-to-buffer buffer)))

(defun my-exwm-move-buffer-left ()
  (interactive)
  (let ((buffer (current-buffer)))
    (evil-window-left 1)
    (switch-to-buffer buffer)))

(defun my-exwm-move-buffer-up ()
  (interactive)
  (let ((buffer (current-buffer)))
    (evil-window-up 1)
    (switch-to-buffer buffer)))

(defun my-exwm-move-buffer-down ()
  (interactive)
  (let ((buffer (current-buffer)))
    (evil-window-down 1)
    (switch-to-buffer buffer)))

Cycling Buffers

These functions are for cycling through EXWM buffers in an (Emacs) window.

my-exwm-buffers-prev is bound to s-[ and my-exwm-buffers-next is bound to s-] allowing me to quickly cycle through a specific set of EXWM buffers in a specific window.

(Actually, I have rebound my parens to where the brackets are on my keyboard and I've bound the functions to s-( and s-).)

;; https://stackoverflow.com/a/2238589/206767
(defun buffer-mode (buffer-or-string)
  "Returns the major mode associated with a buffer."
  (with-current-buffer buffer-or-string
     major-mode))

(defun exwm-mode-p (buffer)
  (string= "exwm-mode" (buffer-mode buffer)))

(defun internal-buffer-p (buffer)
  (string-prefix-p " *" (buffer-name buffer)))

(defun special-buffer-p (buffer)
  (string-prefix-p "*" (buffer-name buffer)))

(defun my-exwm-buffers-next ()
  (interactive)
  (let* ((win (selected-window))
         (bufs (loop for buf in (gethash win my-exwm-buffers-hash-table)
                     unless (string= (buffer-name buf) "nil")
                       collect buf)))
    ;(message "next=%s" bufs)
    (when (> (length bufs) 1)
      (setf bufs (append (cdr bufs) (list (car bufs))))
      (setf (gethash win my-exwm-buffers-hash-table) bufs))
    (when (>= (length bufs) 1)
      (switch-to-buffer (first bufs)))))

(defun my-exwm-buffers-prev ()
  (interactive)
  (let* ((win (selected-window))
         (bufs (loop for buf in (gethash win my-exwm-buffers-hash-table)
                     unless (string= (buffer-name buf) "nil")
                       collect buf)))
    ;(message "prev=%s" bufs)
    (when (> (length bufs) 1)
      (setf bufs (append (last bufs) (butlast bufs)))
      (setf (gethash win my-exwm-buffers-hash-table) bufs))
    (when (>= (length bufs) 1)
      (switch-to-buffer (first bufs)))))

(setq my-exwm-buffers-hash-table (make-hash-table))

(defun my-exwm-buffers-update ()
  (interactive)
  (let ((buf (current-buffer))
        (win (selected-window)))
    (unless (or (gethash win my-exwm-buffers-hash-table)
                (internal-buffer-p buf)
                (special-buffer-p buf)
                (not (exwm-mode-p buf)))
      ;; Create window buffer-list if it does not exist.
      (setf (gethash win my-exwm-buffers-hash-table) (list (current-buffer))))
    (unless (or (internal-buffer-p buf)
                (special-buffer-p buf)
                (not (exwm-mode-p buf)))
      ;; Remove buffer from other window buffer-lists.
      (maphash (lambda (k v)
                 (setf (gethash k my-exwm-buffers-hash-table)
                       (remove (current-buffer) (gethash k my-exwm-buffers-hash-table))))
               my-exwm-buffers-hash-table)
      ;; Add it to the current window buffer-list.
      (push (current-buffer) (gethash win my-exwm-buffers-hash-table)))))

(add-hook 'window-configuration-change-hook 'my-exwm-buffers-update)
my-exwm-functions.el 3.14 KB
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment