Files
@ 443938b472d4
Branch filter:
Location: workgroups2/workgroups-wconfig-restore.el
443938b472d4
5.5 KiB
text/x-elisp
initial commit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | ;;; wconfig restoration
(defun wg-restore-default-buffer ()
"Switch to `wg-default-buffer'."
(switch-to-buffer wg-default-buffer t))
(defun wg-restore-existing-buffer (buf)
"Switch to and return BUF's referrent (some live buffer) if it exists."
(wg-awhen (wg-find-buf-in-buffer-list buf (buffer-list))
(switch-to-buffer it t)
(wg-set-buffer-uid-or-error (wg-buf-uid buf))
it))
(defun wg-restore-file-buffer (buf)
"Restore BUF by finding its file. Return the created buffer.
If BUF's file doesn't exist, call `wg-restore-default-buffer'"
(wg-when-let ((file-name (wg-buf-file-name buf)))
(cond ((file-exists-p file-name)
(find-file file-name)
(rename-buffer (wg-buf-name buf) t)
(wg-set-buffer-uid-or-error (wg-buf-uid buf))
(when wg-restore-mark
(set-mark (wg-buf-mark buf))
(deactivate-mark))
(wg-deserialize-buffer-local-variables buf)
(current-buffer))
(t
(message "Attempt to restore nonexistent file: %S" file-name)
nil))))
(defun wg-restore-special-buffer (buf)
"Restore a buffer with DESERIALIZER-FN."
(wg-when-let
((special-data (wg-buf-special-data buf))
(buffer (save-window-excursion
(condition-case err
(funcall (car special-data) buf)
(error (message "Error deserializing %S: %S"
(wg-buf-name buf) err)
nil)))))
(switch-to-buffer buffer t)
(wg-set-buffer-uid-or-error (wg-buf-uid buf))
buffer))
(defun wg-restore-buffer (buf)
"Restore BUF and return it."
(let (wg-buffer-auto-association-on)
(or (wg-restore-existing-buffer buf)
(wg-restore-special-buffer buf)
(wg-restore-file-buffer buf)
(progn (wg-restore-default-buffer) nil))))
(defun wg-restore-window-positions (win &optional window)
"Restore various positions in WINDOW from their values in WIN."
(let ((window (or window (selected-window))))
(wg-with-slots win
((win-point wg-win-point)
(win-start wg-win-start)
(win-hscroll wg-win-hscroll))
(set-window-start window win-start t)
(set-window-hscroll window win-hscroll)
(set-window-point
window
(cond ((not wg-restore-point) win-start)
((eq win-point :max) (point-max))
(t win-point)))
(when (>= win-start (point-max)) (recenter)))))
;; FIXME: nix these or move them to the vars section
(defvar wg-incorrectly-restored-bufs nil
"FIXME: docstring this")
(defvar wg-record-incorrectly-restored-bufs nil
"FIXME: docstring this")
(defun wg-restore-window (win)
"Restore WIN in `selected-window'."
(let ((selwin (selected-window))
(buf (wg-find-buf-by-uid (wg-win-buf-uid win))))
(if (not buf) (wg-restore-default-buffer)
(when (wg-restore-buffer buf)
(wg-restore-window-positions win selwin)
(when wg-restore-window-dedicated-p
(set-window-dedicated-p selwin (wg-win-dedicated win)))))))
(defun wg-reset-window-tree ()
"Delete all but one window in `selected-frame', and reset
various parameters of that window in preparation for restoring
a wtree."
(delete-other-windows)
(set-window-dedicated-p nil nil))
(defun wg-restore-window-tree-helper (w)
"Recursion helper for `wg-restore-window-tree'."
(if (wg-wtree-p w)
(loop with dir = (wg-wtree-dir w)
for (win . rest) on (wg-wtree-wlist w)
do (when rest (split-window nil (wg-w-size win dir) (not dir)))
do (wg-restore-window-tree-helper win))
(wg-restore-window w)
(when (wg-win-selected w)
(setq wg-window-tree-selected-window (selected-window)))
(when (wg-win-minibuffer-scroll w)
(setq minibuffer-scroll-window (selected-window)))
(other-window 1)))
(defun wg-restore-window-tree (wtree)
"Restore WTREE in `selected-frame'."
(let ((window-min-width wg-window-min-width)
(window-min-height wg-window-min-height)
(wg-window-tree-selected-window nil))
(wg-reset-window-tree)
(wg-restore-window-tree-helper wtree)
(wg-awhen wg-window-tree-selected-window (select-window it))))
(defun wg-wconfig-restore-frame-position (wconfig)
"Restore `selected-frame's position from WCONFIG."
(wg-when-let ((left (wg-wconfig-left wconfig))
(top (wg-wconfig-top wconfig)))
(set-frame-position (selected-frame) left top)))
(defun wg-wconfig-restore-scroll-bars (wconfig)
"Restore `selected-frame's scroll-bar settings from WCONFIG."
(set-frame-parameter
nil 'vertical-scroll-bars (wg-wconfig-vertical-scroll-bars wconfig))
(set-frame-parameter
nil 'scroll-bar-width (wg-wconfig-scroll-bar-width wconfig)))
;; FIXME: throw a specific error if the restoration was unsuccessful
(defun wg-restore-wconfig (wconfig)
"Restore WCONFIG in `selected-frame'."
(let ((wg-record-incorrectly-restored-bufs t)
(wg-incorrectly-restored-bufs nil))
(wg-barf-on-active-minibuffer)
(when wg-restore-frame-position
(wg-wconfig-restore-frame-position wconfig))
(let ((wtree (wg-resize-frame-scale-wtree wconfig)))
(wg-restore-window-tree
(if (not (wg-morph-p)) wtree (wg-morph wtree))))
(when wg-restore-scroll-bars
(wg-wconfig-restore-scroll-bars wconfig))
(when wg-incorrectly-restored-bufs
(message "Unable to restore these buffers: %S\
If you want, restore them manually and try again."
(mapcar 'wg-buf-name wg-incorrectly-restored-bufs)))))
(provide 'workgroups-wconfig-restore)
|