From 16db26c60b1be680cf8873f89208bfd7a4765e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 16 Oct 2021 20:33:15 +0200 Subject: Emacs: Associate layout with individual buffers instead --- .emacs.d/init.el | 79 +++++++++++++++++++++++--------------------------------- 1 file changed, 32 insertions(+), 47 deletions(-) diff --git a/.emacs.d/init.el b/.emacs.d/init.el index a28c2b4..15c8a03 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -186,54 +186,39 @@ ;; (add-to-list 'ibuffer-never-show-predicates "^magit-") ;;; Layouts -(defvar *layout-stack* (list) "Window layout stack") -(defvar *active-layout* -1 "Active window layout") -(defun layout-load-fun (reg) - (lambda () - (interactive) - (when (not (= *active-layout* reg)) - (set-register *active-layout* (current-window-configuration)) - (set-window-configuration (get-register reg)) - (when *active-layout* - (push *layout-stack* *active-layout*)) - (setq *active-layout* reg) - (message (format-message "Loaded layout %c." reg))))) -(defun layout-save-fun (reg) - (lambda () - (interactive) - (when (not (= *active-layout* reg)) - (setq *active-layout* reg) - (message (format-message "Saved layout %c." reg))))) -(defun which-layout () +(defvar *buffer-layouts* (list) "Buffer-layout associations") +(defun restore-buffer-layout () + "Restore the layout associated with the current buffer." (interactive) - (message (format-message "Current layout: %c" *active-layout*))) -(set-keys "C-c ยง" which-layout - "C-c 1" (layout-load-fun ?1) - "C-c M-1" (layout-save-fun ?1) - "C-c 2" (layout-load-fun ?2) - "C-c M-2" (layout-save-fun ?2) - "C-c 3" (layout-load-fun ?3) - "C-c M-3" (layout-save-fun ?3) - "C-c 4" (layout-load-fun ?4) - "C-c M-4" (layout-save-fun ?4) - "C-c 5" (layout-load-fun ?5) - "C-c M-5" (layout-save-fun ?5) - "C-c 6" (layout-load-fun ?6) - "C-c M-6" (layout-save-fun ?6) - "C-c 7" (layout-load-fun ?7) - "C-c M-7" (layout-save-fun ?7) - "C-c 8" (layout-load-fun ?8) - "C-c M-8" (layout-save-fun ?8) - "C-c 9" (layout-load-fun ?9) - "C-c M-9" (layout-save-fun ?9) - "C-c 0" (layout-load-fun ?0) - "C-c M-0" (layout-save-fun ?0)) - ; In a way, the only thing - ; that is needed is to hook - ; into the "create other - ; window" hook and save the - ; previous layout and the new - ; layout. + (let ((conf (alist-get (current-buffer) *buffer-layouts*))) + (when (not conf) + (error "No layout associated with buffer.")) + (set-window-configuration conf) + (message "Restored buffer layout."))) +(defun switch-to-buffer-with-layout () + "Switch to the window layout associated with a buffer. At the +same time, associate the original buffer with the original +layout. + +If the new buffer has no associated layout, it is displayed as +the only window in the frame." + (interactive) + (dolist (window (window-list)) + (setf (alist-get (window-buffer window) *buffer-layouts*) + (current-window-configuration))) + (call-interactively #'helm-multi-files) + (delete-other-windows) + (let* ((buf (current-buffer)) + (conf (alist-get buf *buffer-layouts*))) + (when conf + (set-window-configuration conf) + (select-window (get-buffer-window buf))))) +(defun delete-layout-before-delete-window (window) + (setf (alist-get (window-buffer window) *buffer-layouts*) nil)) +(advice-add #'delete-window :before #'delete-layout-before-delete-window) +(advice-add #'quit-window :before #'delete-layout-before-delete-window) +(set-keys "C-c b" switch-to-buffer-with-layout + "C-c n" restore-buffer-layout) ;;; Lisp (autoload #'enable-paredit-mode "paredit" nil t) -- cgit v1.2.3