From 688ba7ee95e9e07e4991e647184e95e8e43be829 Mon Sep 17 00:00:00 2001 From: Bar Magal Date: Thu, 9 Jul 2015 16:22:27 +0300 Subject: [PATCH 1/5] Fix text vs. total window width handling #22 --- which-key.el | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/which-key.el b/which-key.el index 35147934208..8675aba6e72 100644 --- a/which-key.el +++ b/which-key.el @@ -220,6 +220,42 @@ Finally, show the buffer." ;; command finished maybe close the window ;; (which-key/hide-popup)))) +;; window-size utilities + +(defun which-key/text-width-to-total (text-width) + "Convert window text-width to window total-width. +TEXT-WIDTH is the desired text width of the window. The function calculates what +total width is required for a window in the selected to have a text-width of +TEXT-WIDTH columns. The calculation considers possible fringes and scroll bars. +This function assumes that the desired window has the same character width as +the frame." + (let ((char-width (frame-char-width))) + (+ text-width + (/ (frame-fringe-width) char-width) + (/ (frame-scroll-bar-width) char-width) + (if (which-key/char-enlarged-p) 1 0)))) + +(defun which-key/total-width-to-text (total-width) + "Convert window total-width to window text-width. +TOTAL-WIDTH is the desired total width of the window. The function calculates +what text width fits such a window. The calculation considers possible fringes +and scroll bars. This function assumes that the desired window has the same +character width as the frame." + (let ((char-width (frame-char-width))) + (- total-width + (/ (frame-fringe-width) char-width) + (/ (frame-scroll-bar-width) char-width) + (if (which-key/char-enlarged-p) 1 0)))) + +(defun which-key/char-enlarged-p (&optional frame) + (> (frame-char-width) (/ (float (frame-pixel-width)) (window-total-width (frame-root-window))))) + +(defun which-key/char-reduced-p (&optional frame) + (< (frame-char-width) (/ (float (frame-pixel-width)) (window-total-width (frame-root-window))))) + +(defun which-key/char-exact-p (&optional frame) + (= (frame-char-width) (/ (float (frame-pixel-width)) (window-total-width (frame-root-window))))) + ;; Show/hide guide buffer (defun which-key/hide-popup () @@ -257,7 +293,7 @@ need to start the closing timer." (defun which-key/show-buffer-side-window (act-popup-dim) (let* ((height (car act-popup-dim)) - (width (cdr act-popup-dim)) + (width (which-key/text-width-to-total (cdr act-popup-dim))) (side which-key-side-window-location) (alist (delq nil (list (when height (cons 'window-height height)) (when width (cons 'window-width width)))))) @@ -277,11 +313,8 @@ need to start the closing timer." ;; (display-buffer which-key--buffer (cons 'display-buffer-in-side-window alist)) ;; side defaults to bottom (if (get-buffer-window which-key--buffer) - (progn - (display-buffer-reuse-window which-key--buffer alist)) - (display-buffer-in-major-side-window which-key--buffer side 0 alist)) - (let ((fit-window-to-buffer-horizontally t)) - (fit-window-to-buffer (get-buffer-window which-key--buffer))))) + (display-buffer-reuse-window which-key--buffer alist) + (display-buffer-in-major-side-window which-key--buffer side 0 alist)))) (defun which-key/show-buffer-frame (act-popup-dim) (let* ((orig-window (selected-window)) @@ -382,8 +415,8 @@ of the intended popup." which-key-side-window-max-height) ;; width (if (member which-key-side-window-location '(left right)) - which-key-side-window-max-width - (frame-width)))) + (which-key/total-width-to-text which-key-side-window-max-width) + (window-width (frame-root-window))))) (defun which-key/frame-max-dimensions () (cons which-key-frame-max-height which-key-frame-max-width)) From d9a9bd5ae6172d6cc8a6f1bdf6d2608ad3c885b8 Mon Sep 17 00:00:00 2001 From: Bar Magal Date: Thu, 9 Jul 2015 17:27:11 +0300 Subject: [PATCH 2/5] Allow percentages for side-window max sizes #19 --- which-key.el | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/which-key.el b/which-key.el index 8675aba6e72..aa4a2897605 100644 --- a/which-key.el +++ b/which-key.el @@ -78,10 +78,14 @@ the feature off.") side-window. Should be one of top, bottom, left or right.") (defvar which-key-side-window-max-width 60 "Maximum width of which-key popup when type is side-window and -location is left or right.") +location is left or right. +This variable can also be a number between 0 and 1. In that case, it denotes +a percentage out of the frame's width.") (defvar which-key-side-window-max-height 20 "Maximum height of which-key popup when type is side-window and -location is top or bottom.") +location is top or bottom. +This variable can also be a number between 0 and 1. In that case, it denotes +a percentage out of the frame's height.") (defvar which-key-frame-max-width 60 "Maximum width of which-key popup when type is frame.") (defvar which-key-frame-max-height 20 @@ -256,6 +260,26 @@ character width as the frame." (defun which-key/char-exact-p (&optional frame) (= (frame-char-width) (/ (float (frame-pixel-width)) (window-total-width (frame-root-window))))) +(defun which-key/width-or-percentage-to-width (width-or-percentage) + "Return window total width. +If WIDTH-OR-PERCENTAGE is a whole number, return it unchanged. Otherwise, it +should be a percentage (a number between 0 and 1) out of the frame's width. +More precisely, it should be a percentage out of the frame's root window's +total width." + (if (wholenump width-or-percentage) + width-or-percentage + (round (* width-or-percentage (window-total-width (frame-root-window)))))) + +(defun which-key/height-or-percentage-to-height (height-or-percentage) + "Return window total height. +If HEIGHT-OR-PERCENTAGE is a whole number, return it unchanged. Otherwise, it +should be a percentage (a number between 0 and 1) out of the frame's height. +More precisely, it should be a percentage out of the frame's root window's +total height." + (if (wholenump height-or-percentage) + height-or-percentage + (round (* height-or-percentage (window-total-height (frame-root-window)))))) + ;; Show/hide guide buffer (defun which-key/hide-popup () @@ -412,10 +436,11 @@ of the intended popup." ;; (window-height (minibuffer-window)) ;; (window-mode-line-height which-key--window)) ;; FIXME: change to something like (min which-*-height (calculate-max-height)) - which-key-side-window-max-height) + (which-key/height-or-percentage-to-height which-key-side-window-max-height)) ;; width (if (member which-key-side-window-location '(left right)) - (which-key/total-width-to-text which-key-side-window-max-width) + (which-key/total-width-to-text (which-key/width-or-percentage-to-width + which-key-side-window-max-width)) (window-width (frame-root-window))))) (defun which-key/frame-max-dimensions () From 315eeca54dbceeb240f7056ddc034a0c1b82f870 Mon Sep 17 00:00:00 2001 From: Bar Magal Date: Thu, 9 Jul 2015 18:56:36 +0300 Subject: [PATCH 3/5] Use fit-window-to-buffer again --- which-key.el | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/which-key.el b/which-key.el index aa4a2897605..a95d2cef00a 100644 --- a/which-key.el +++ b/which-key.el @@ -237,7 +237,9 @@ the frame." (+ text-width (/ (frame-fringe-width) char-width) (/ (frame-scroll-bar-width) char-width) - (if (which-key/char-enlarged-p) 1 0)))) + (if (which-key/char-enlarged-p) 1 0) + ;; add padding to account for possible wide (unicode) characters + 3))) (defun which-key/total-width-to-text (total-width) "Convert window total-width to window text-width. @@ -249,7 +251,9 @@ character width as the frame." (- total-width (/ (frame-fringe-width) char-width) (/ (frame-scroll-bar-width) char-width) - (if (which-key/char-enlarged-p) 1 0)))) + (if (which-key/char-enlarged-p) 1 0) + ;; add padding to account for possible wide (unicode) characters + 3))) (defun which-key/char-enlarged-p (&optional frame) (> (frame-char-width) (/ (float (frame-pixel-width)) (window-total-width (frame-root-window))))) @@ -315,12 +319,16 @@ need to start the closing timer." (defun which-key/show-buffer-minibuffer (act-popup-dim) nil) -(defun which-key/show-buffer-side-window (act-popup-dim) - (let* ((height (car act-popup-dim)) - (width (which-key/text-width-to-total (cdr act-popup-dim))) - (side which-key-side-window-location) - (alist (delq nil (list (when height (cons 'window-height height)) - (when width (cons 'window-width width)))))) +;; &rest params because `fit-buffer-to-window' has a different call signature +;; in different emacs versions +(defun which-key/fit-buffer-to-window-horizontally (&optional window &rest params) + (let ((fit-window-to-buffer-horizontally t)) + (apply #'fit-window-to-buffer window params))) + +(defun which-key/show-buffer-side-window (_act-popup-dim) + (let* ((side which-key-side-window-location) + (alist '((window-width . which-key/fit-buffer-to-window-horizontally) + (window-height . fit-window-to-buffer)))) ;; Note: `display-buffer-in-side-window' and `display-buffer-in-major-side-window' ;; were added in Emacs 24.3 From d0a9cc0c10773fe6c579ac4864f467a13742e550 Mon Sep 17 00:00:00 2001 From: justbur Date: Thu, 9 Jul 2015 11:12:19 -0400 Subject: [PATCH 4/5] Change side-window params to percentages --- which-key.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/which-key.el b/which-key.el index aa4a2897605..ace968ac4bf 100644 --- a/which-key.el +++ b/which-key.el @@ -76,12 +76,12 @@ the feature off.") (defvar which-key-side-window-location 'right "Location of which-key popup when `which-key-popup-type' is side-window. Should be one of top, bottom, left or right.") -(defvar which-key-side-window-max-width 60 +(defvar which-key-side-window-max-width 0.333 "Maximum width of which-key popup when type is side-window and location is left or right. This variable can also be a number between 0 and 1. In that case, it denotes a percentage out of the frame's width.") -(defvar which-key-side-window-max-height 20 +(defvar which-key-side-window-max-height 0.25 "Maximum height of which-key popup when type is side-window and location is top or bottom. This variable can also be a number between 0 and 1. In that case, it denotes From a73d8b8fadc505a7e3bc6d7373ad2a118d9a4b28 Mon Sep 17 00:00:00 2001 From: justbur Date: Thu, 9 Jul 2015 12:24:19 -0400 Subject: [PATCH 5/5] remove mode-line --- which-key.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/which-key.el b/which-key.el index 8a810633085..e36fcc60454 100644 --- a/which-key.el +++ b/which-key.el @@ -161,7 +161,7 @@ Used when `which-key-popup-type' is frame.") (with-current-buffer which-key--buffer (setq-local cursor-type nil) (setq-local cursor-in-non-selected-windows nil) - (setq-local mode-line-format "")) + (setq-local mode-line-format nil)) (setq which-key--setup-p t)) ;;;###autoload