Skip to content

Commit 207df83

Browse files
committed
Display the help menu in a side window
Exclude the help menu when saving a configuration. When restoring a configuration display the help menu only if it was currently open. Workaround to force update the current buffer values. Soon after `ivy-switch-buffer`, both `current-buffer` and `get-buffer-window` still reference the old buffer, not the one switched to.
1 parent 584511f commit 207df83

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

resize-window.el

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ should return the fine adjustment (default 1)."
133133
(?0 resize-window--delete-window " Delete window" nil)
134134
(?k resize-window--kill-other-windows " Kill other windows (save state)" nil)
135135
(?y resize-window--restore-windows " (when state) Restore window configuration" nil)
136-
(?? resize-window--display-menu " Resize - display menu" nil))
136+
(?? resize-window--display-menu " Resize - toggle help menu" nil))
137137
"List of resize mode bindings.
138138
Main data structure of the dispatcher with the form:
139139
\(key function documentation allows-capitals\)")
@@ -260,9 +260,12 @@ to resize right."
260260
;; with t and that method can worry about how to get that
261261
;; action
262262
(resize-window--execute-action capital t))
263-
(t (setq reading-characters nil)
264-
(resize-window--remove-backgrounds))))))
263+
(t
264+
(setq reading-characters nil)
265+
(resize-window--display-menu 'kill)
266+
(resize-window--remove-backgrounds))))))
265267
(quit
268+
(resize-window--display-menu 'kill)
266269
(resize-window--remove-backgrounds))))
267270

268271
;;; Function Handlers
@@ -312,9 +315,33 @@ If no SIZE is given, modify by `resize-window-default-argument'"
312315
(other-window -1)
313316
(resize-window--add-backgrounds))
314317

315-
(defun resize-window--display-menu ()
316-
"Display menu in minibuffer."
317-
(resize-window--notify "%s" (resize-window--get-documentation-strings)))
318+
(defun resize-window--display-menu (&optional action)
319+
"Toggle help menu side window or perform ACTION if non-nil.
320+
ACTION is a symbol of value 'kill or 'open."
321+
(let* ((buffer (get-buffer-create "*Resize-Window-Help*"))
322+
(window (get-buffer-window buffer))
323+
(add-backgrounds nil))
324+
(cond
325+
((and window (or (not action) (eq action 'kill)))
326+
(quit-window t window)
327+
(setq add-backgrounds t))
328+
((and (not window) (or (not action) (eq action 'open)))
329+
(setq window (display-buffer-in-side-window buffer nil))
330+
(set-window-parameter window 'no-other-window t)
331+
(set-window-parameter window 'no-delete-other-windows t)
332+
(with-current-buffer buffer
333+
(setq buffer-read-only t)
334+
(setq window-size-fixed t)
335+
(let ((inhibit-read-only t)
336+
(window-size-fixed nil))
337+
(erase-buffer)
338+
(insert (resize-window--get-documentation-strings))
339+
(fit-window-to-buffer window)))
340+
(setq add-backgrounds t)))
341+
;; NOTE: Just in case the help menu was selected (it shouldn't)
342+
;; refresh the backgrounds even when the help menu is killed.
343+
(when add-backgrounds
344+
(resize-window--add-backgrounds))))
318345

319346
(defun resize-window--split-window-below ()
320347
"Split the window vertically."
@@ -332,9 +359,42 @@ If no SIZE is given, modify by `resize-window-default-argument'"
332359
(delete-window)
333360
(resize-window--add-backgrounds)))
334361

362+
(defun resize-window--window-config ()
363+
"Return the current window configuration.
364+
Exclude the help menu from the configuration."
365+
(let ((display-menu (get-buffer-window "*Resize-Window-Help*")))
366+
(resize-window--display-menu 'kill)
367+
(prog2
368+
;; WORKAROUND: Calling `current-buffer' or `get-buffer-window'
369+
;; soon after `ivy-switch-buffer' references the old buffer.
370+
;; This forces to update to the buffer switched to. It also
371+
;; allows `current-window-configuration' to capture a proper
372+
;; configuration updating the values of all current buffers.
373+
;; See also https://github.com/abo-abo/swiper/issues/1766
374+
(let ((curr-window (selected-window)))
375+
(mapc (lambda (w) (select-window w)) (window-list))
376+
(select-window curr-window))
377+
(current-window-configuration)
378+
(when display-menu
379+
(resize-window--display-menu 'open)))))
380+
381+
(defun resize-window--restore-config (config)
382+
"Restore the window configuration CONFIG then return it.
383+
Restore the help menu only if it is currently open."
384+
(let ((display-menu (get-buffer-window "*Resize-Window-Help*")))
385+
(set-window-configuration config)
386+
;; NOTE: If `resize-window--window-config' was used to save the
387+
;; CONFIG there is no help menu to kill. Keep this just in case.
388+
(resize-window--display-menu 'kill)
389+
(prog1
390+
(current-window-configuration)
391+
(if display-menu
392+
(resize-window--display-menu 'open)
393+
(resize-window--add-backgrounds)))))
394+
335395
(defun resize-window--window-push ()
336396
"Save the current state in the stack."
337-
(push (current-window-configuration) resize-window--window-stack))
397+
(push (resize-window--window-config) resize-window--window-stack))
338398

339399
(defun resize-window--window-pop ()
340400
"Return the first element and remove it from the stack."
@@ -350,8 +410,7 @@ If no SIZE is given, modify by `resize-window-default-argument'"
350410
"Restore the previous state."
351411
(let ((config (resize-window--window-pop)))
352412
(when config
353-
(set-window-configuration config)
354-
(resize-window--add-backgrounds))))
413+
(resize-window--restore-config config))))
355414

356415
(defvar resize-window--capital-letters (number-sequence ?A ?Z)
357416
"List of uppercase letters as characters.")

0 commit comments

Comments
 (0)