From 61ff12cbfc1daf39a63d6597982c6f7b6c4db7a4 Mon Sep 17 00:00:00 2001 From: duianto Date: Sun, 3 Jan 2021 19:38:15 +0100 Subject: [PATCH] [base] Show a winum required message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit problem: key bindings that use the winum package, don't work in the spacemacs-base distribution. for example in the buffer transient state: SPC b . {number}, C-{number} or M-{number} shows messages like these: Wrong type argument: commandp, winum-select-window-1 Symbol’s function definition is void: winum-get-window-by-number cause: the winum package is loaded in the spacemacs-navigation layer, but the layer isn't used in the spacemacs-base distribution. solution: show the message: This command requires the winum package, winum is part of the spacemacs-navigation layer. --- layers/+filetree/neotree/packages.el | 15 +-- layers/+filetree/treemacs/packages.el | 25 ++-- layers/+os/osx/keybindings.el | 18 +-- layers/+source-control/git/packages.el | 18 +-- layers/+spacemacs/spacemacs-defaults/funcs.el | 107 +++++++++++------- .../spacemacs-defaults/keybindings.el | 38 +++---- 6 files changed, 127 insertions(+), 94 deletions(-) diff --git a/layers/+filetree/neotree/packages.el b/layers/+filetree/neotree/packages.el index 64f80b743..4908e8237 100644 --- a/layers/+filetree/neotree/packages.el +++ b/layers/+filetree/neotree/packages.el @@ -116,10 +116,11 @@ Navigation^^^^ Actions^^ Visual actions/config^^^ (defun neotree/pre-init-winum () (spacemacs|use-package-add-hook winum :post-config - ;; `0', `M-0' and `C-x w 0' are bound to `winum-select-window-0-or-10' - (define-key winum-keymap [remap winum-select-window-0-or-10] #'neotree-show) - ;; replace the which-key name - (push '((nil . "winum-select-window-0-or-10") . (nil . "neotree-show")) - which-key-replacement-alist) - (add-to-list 'winum-assign-functions - #'spacemacs//winum-neotree-assign-func))) + (when (configuration-layer/package-used-p 'winum) + ;; `0', `M-0' and `C-x w 0' are bound to `winum-select-window-0-or-10' + (define-key winum-keymap [remap winum-select-window-0-or-10] #'neotree-show) + ;; replace the which-key name + (push '((nil . "winum-select-window-0-or-10") . (nil . "neotree-show")) + which-key-replacement-alist) + (add-to-list 'winum-assign-functions + #'spacemacs//winum-neotree-assign-func)))) diff --git a/layers/+filetree/treemacs/packages.el b/layers/+filetree/treemacs/packages.el index e7fe5ab1f..55bbb39ca 100644 --- a/layers/+filetree/treemacs/packages.el +++ b/layers/+filetree/treemacs/packages.el @@ -109,18 +109,19 @@ (spacemacs|use-package-add-hook winum :post-config (progn - ;; `0', `M-0' and `C-x w 0' are bound to `winum-select-window-0-or-10' - (define-key winum-keymap - [remap winum-select-window-0-or-10] #'treemacs-select-window) - ;; replace the which-key name - (push '((nil . "winum-select-window-0-or-10") . - (nil . "treemacs-select-window")) - which-key-replacement-alist) - (with-eval-after-load 'treemacs - (dolist (n (number-sequence 1 5)) - (add-to-list 'winum-ignored-buffers - (format "%sFramebuffer-%s*" - treemacs--buffer-name-prefix n))))))) + (when (configuration-layer/package-used-p 'winum) + ;; `0', `M-0' and `C-x w 0' are bound to `winum-select-window-0-or-10' + (define-key winum-keymap + [remap winum-select-window-0-or-10] #'treemacs-select-window) + ;; replace the which-key name + (push '((nil . "winum-select-window-0-or-10") . + (nil . "treemacs-select-window")) + which-key-replacement-alist) + (with-eval-after-load 'treemacs + (dolist (n (number-sequence 1 5)) + (add-to-list 'winum-ignored-buffers + (format "%sFramebuffer-%s*" + treemacs--buffer-name-prefix n)))))))) (defun treemacs/init-treemacs-magit () (use-package treemacs-magit diff --git a/layers/+os/osx/keybindings.el b/layers/+os/osx/keybindings.el index b0f0cc893..a82473f35 100644 --- a/layers/+os/osx/keybindings.el +++ b/layers/+os/osx/keybindings.el @@ -88,15 +88,15 @@ default." (call-interactively (key-binding "\C-x\C-s")))) ;; window manipulation with command key - (global-set-key (kbd-mac-command "1") 'winum-select-window-1) - (global-set-key (kbd-mac-command "2") 'winum-select-window-2) - (global-set-key (kbd-mac-command "3") 'winum-select-window-3) - (global-set-key (kbd-mac-command "4") 'winum-select-window-4) - (global-set-key (kbd-mac-command "5") 'winum-select-window-5) - (global-set-key (kbd-mac-command "6") 'winum-select-window-6) - (global-set-key (kbd-mac-command "7") 'winum-select-window-7) - (global-set-key (kbd-mac-command "8") 'winum-select-window-8) - (global-set-key (kbd-mac-command "9") 'winum-select-window-9) + (global-set-key (kbd-mac-command "1") 'spacemacs/winum-select-window-1) + (global-set-key (kbd-mac-command "2") 'spacemacs/winum-select-window-2) + (global-set-key (kbd-mac-command "3") 'spacemacs/winum-select-window-3) + (global-set-key (kbd-mac-command "4") 'spacemacs/winum-select-window-4) + (global-set-key (kbd-mac-command "5") 'spacemacs/winum-select-window-5) + (global-set-key (kbd-mac-command "6") 'spacemacs/winum-select-window-6) + (global-set-key (kbd-mac-command "7") 'spacemacs/winum-select-window-7) + (global-set-key (kbd-mac-command "8") 'spacemacs/winum-select-window-8) + (global-set-key (kbd-mac-command "9") 'spacemacs/winum-select-window-9) (global-set-key (kbd-mac-command "Z") 'undo-tree-redo) (global-set-key (kbd-mac-command "C-f") 'spacemacs/toggle-frame-fullscreen) diff --git a/layers/+source-control/git/packages.el b/layers/+source-control/git/packages.el index 8a08e08bc..9ddb9348f 100644 --- a/layers/+source-control/git/packages.el +++ b/layers/+source-control/git/packages.el @@ -263,15 +263,15 @@ ;; Workaround for #12747 - org-mode (evil-define-key 'normal magit-blame-read-only-mode-map (kbd "RET") 'magit-show-commit) ;; Make sure that M-m still switch windows in all magit buffers - (evil-define-key 'normal magit-section-mode-map (kbd "M-1") 'winum-select-window-1) - (evil-define-key 'normal magit-section-mode-map (kbd "M-2") 'winum-select-window-2) - (evil-define-key 'normal magit-section-mode-map (kbd "M-3") 'winum-select-window-3) - (evil-define-key 'normal magit-section-mode-map (kbd "M-4") 'winum-select-window-4) - (evil-define-key 'normal magit-section-mode-map (kbd "M-5") 'winum-select-window-5) - (evil-define-key 'normal magit-section-mode-map (kbd "M-6") 'winum-select-window-6) - (evil-define-key 'normal magit-section-mode-map (kbd "M-7") 'winum-select-window-7) - (evil-define-key 'normal magit-section-mode-map (kbd "M-8") 'winum-select-window-8) - (evil-define-key 'normal magit-section-mode-map (kbd "M-9") 'winum-select-window-9)))) + (evil-define-key 'normal magit-section-mode-map (kbd "M-1") 'spacemacs/winum-select-window-1) + (evil-define-key 'normal magit-section-mode-map (kbd "M-2") 'spacemacs/winum-select-window-2) + (evil-define-key 'normal magit-section-mode-map (kbd "M-3") 'spacemacs/winum-select-window-3) + (evil-define-key 'normal magit-section-mode-map (kbd "M-4") 'spacemacs/winum-select-window-4) + (evil-define-key 'normal magit-section-mode-map (kbd "M-5") 'spacemacs/winum-select-window-5) + (evil-define-key 'normal magit-section-mode-map (kbd "M-6") 'spacemacs/winum-select-window-6) + (evil-define-key 'normal magit-section-mode-map (kbd "M-7") 'spacemacs/winum-select-window-7) + (evil-define-key 'normal magit-section-mode-map (kbd "M-8") 'spacemacs/winum-select-window-8) + (evil-define-key 'normal magit-section-mode-map (kbd "M-9") 'spacemacs/winum-select-window-9)))) (defun git/init-magit-delta () (use-package magit-delta diff --git a/layers/+spacemacs/spacemacs-defaults/funcs.el b/layers/+spacemacs/spacemacs-defaults/funcs.el index 519bf55a5..0516ef44f 100644 --- a/layers/+spacemacs/spacemacs-defaults/funcs.el +++ b/layers/+spacemacs/spacemacs-defaults/funcs.el @@ -257,55 +257,86 @@ Dedicated (locked) windows are left untouched." (interactive "p") (spacemacs/rotate-windows-forward (* -1 count))) -(defun spacemacs/move-buffer-to-window (windownum follow-focus-p) - "Moves a buffer to a window, using the spacemacs numbering. follow-focus-p -controls whether focus moves to new window (with buffer), or stays on current" - (interactive) - (if (> windownum (length (window-list-1 nil nil t))) - (message "No window numbered %s" windownum) - (let ((b (current-buffer)) - (w1 (selected-window)) - (w2 (winum-get-window-by-number windownum))) - (unless (eq w1 w2) - (set-window-buffer w2 b) - (switch-to-prev-buffer) - (unrecord-window-buffer w1 b)) - (when follow-focus-p - (select-window (winum-get-window-by-number windownum)))))) -(defun spacemacs/swap-buffers-to-window (windownum follow-focus-p) - "Swaps visible buffers between active window and selected window. -follow-focus-p controls whether focus moves to new window (with buffer), or -stays on current" - (interactive) - (if (> windownum (length (window-list-1 nil nil t))) - (message "No window numbered %s" windownum) - (let* ((b1 (current-buffer)) - (w1 (selected-window)) - (w2 (winum-get-window-by-number windownum)) - (b2 (window-buffer w2))) - (unless (eq w1 w2) - (set-window-buffer w1 b2) - (set-window-buffer w2 b1) - (unrecord-window-buffer w1 b1) - (unrecord-window-buffer w2 b2))) - (when follow-focus-p (winum-select-window-by-number windownum)))) +(if (configuration-layer/package-used-p 'winum) + (progn + (defun spacemacs/move-buffer-to-window (windownum follow-focus-p) + "Moves a buffer to a window, using the spacemacs numbering. follow-focus-p + controls whether focus moves to new window (with buffer), or stays on current" + (interactive) + (if (> windownum (length (window-list-1 nil nil t))) + (message "No window numbered %s" windownum) + (let ((b (current-buffer)) + (w1 (selected-window)) + (w2 (winum-get-window-by-number windownum))) + (unless (eq w1 w2) + (set-window-buffer w2 b) + (switch-to-prev-buffer) + (unrecord-window-buffer w1 b)) + (when follow-focus-p + (select-window (winum-get-window-by-number windownum)))))) + (defun spacemacs/swap-buffers-to-window (windownum follow-focus-p) + "Swaps visible buffers between active window and selected window. + follow-focus-p controls whether focus moves to new window (with buffer), or + stays on current" + (interactive) + (if (> windownum (length (window-list-1 nil nil t))) + (message "No window numbered %s" windownum) + (let* ((b1 (current-buffer)) + (w1 (selected-window)) + (w2 (winum-get-window-by-number windownum)) + (b2 (window-buffer w2))) + (unless (eq w1 w2) + (set-window-buffer w1 b2) + (set-window-buffer w2 b1) + (unrecord-window-buffer w1 b1) + (unrecord-window-buffer w2 b2))) + (when follow-focus-p (winum-select-window-by-number windownum))))) + ;; when the winum package isn't used + (defun spacemacs//message-winum-package-required () + (interactive) + (message (concat "This command requires the winum package," "\n" + "winum is part of the spacemacs-navigation layer.")))) + +;; define and evaluate numbered functions: +;; spacemacs/winum-select-window-0 to 9 +(dotimes (i 10) + (eval `(defun ,(intern (format "spacemacs/winum-select-window-%s" i)) (&optional arg) + ,(concat (format "Select window %i\n" i) + "Or if the winum package isn't used:\n" + "For example in the spacemacs-base distribution." + "Show a message stating that the winum package," + "is part of the spacemacs-navigation layer.\n") + (interactive "P") + (if (configuration-layer/package-used-p 'winum) + (funcall ',(intern (format "winum-select-window-%s" i)) arg) + (spacemacs//message-winum-package-required))))) + +;; define and evaluate three numbered functions: +;; buffer-to-window-1 to 9 +;; move-buffer-window-no-follow-1 to 9 +;; swap-buffer-window-no-follow-1 to 9 (dotimes (i 9) (let ((n (+ i 1))) (eval `(defun ,(intern (format "buffer-to-window-%s" n)) (&optional arg) ,(format "Move buffer to the window with number %i." n) (interactive "P") - (if arg - (spacemacs/swap-buffers-to-window ,n t) - (spacemacs/move-buffer-to-window ,n t)))) + (if (configuration-layer/package-used-p 'winum) + (if arg + (spacemacs/swap-buffers-to-window ,n t) + (spacemacs/move-buffer-to-window ,n t)) + (spacemacs//message-winum-package-required)))) (eval `(defun ,(intern (format "move-buffer-window-no-follow-%s" n)) () (interactive) - (spacemacs/move-buffer-to-window ,n nil))) + (if (configuration-layer/package-used-p 'winum) + (spacemacs/move-buffer-to-window ,n nil) + (spacemacs//message-winum-package-required)))) (eval `(defun ,(intern (format "swap-buffer-window-no-follow-%s" n)) () (interactive) - (spacemacs/swap-buffers-to-window ,n nil))) - )) + (if (configuration-layer/package-used-p 'winum) + (spacemacs/swap-buffers-to-window ,n nil) + (spacemacs//message-winum-package-required)))))) (defun spacemacs/rename-file (filename &optional new-filename) "Rename FILENAME to NEW-FILENAME. diff --git a/layers/+spacemacs/spacemacs-defaults/keybindings.el b/layers/+spacemacs/spacemacs-defaults/keybindings.el index 2fdc98f11..bda49c7da 100644 --- a/layers/+spacemacs/spacemacs-defaults/keybindings.el +++ b/layers/+spacemacs/spacemacs-defaults/keybindings.el @@ -770,15 +770,15 @@ respond to this toggle." ("M-7" swap-buffer-window-no-follow-7) ("M-8" swap-buffer-window-no-follow-8) ("M-9" swap-buffer-window-no-follow-9) - ("C-1" winum-select-window-1) - ("C-2" winum-select-window-2) - ("C-3" winum-select-window-3) - ("C-4" winum-select-window-4) - ("C-5" winum-select-window-5) - ("C-6" winum-select-window-6) - ("C-7" winum-select-window-7) - ("C-8" winum-select-window-8) - ("C-9" winum-select-window-9)) + ("C-1" spacemacs/winum-select-window-1) + ("C-2" spacemacs/winum-select-window-2) + ("C-3" spacemacs/winum-select-window-3) + ("C-4" spacemacs/winum-select-window-4) + ("C-5" spacemacs/winum-select-window-5) + ("C-6" spacemacs/winum-select-window-6) + ("C-7" spacemacs/winum-select-window-7) + ("C-8" spacemacs/winum-select-window-8) + ("C-9" spacemacs/winum-select-window-9)) (spacemacs/set-leader-keys "b." 'spacemacs/buffer-transient-state/body) ;; end of Buffer Transient State @@ -859,16 +859,16 @@ Select: _a_ _h_ _j_ _k_ _l_ _w_ _0_.._9_ Move: _H_ _J_ _K_ _L_ _r_ _R_ Split: _s ("" evil-window-left) ("l" evil-window-right) ("" evil-window-right) - ("0" winum-select-window-0) - ("1" winum-select-window-1) - ("2" winum-select-window-2) - ("3" winum-select-window-3) - ("4" winum-select-window-4) - ("5" winum-select-window-5) - ("6" winum-select-window-6) - ("7" winum-select-window-7) - ("8" winum-select-window-8) - ("9" winum-select-window-9) + ("0" spacemacs/winum-select-window-0) + ("1" spacemacs/winum-select-window-1) + ("2" spacemacs/winum-select-window-2) + ("3" spacemacs/winum-select-window-3) + ("4" spacemacs/winum-select-window-4) + ("5" spacemacs/winum-select-window-5) + ("6" spacemacs/winum-select-window-6) + ("7" spacemacs/winum-select-window-7) + ("8" spacemacs/winum-select-window-8) + ("9" spacemacs/winum-select-window-9) ("a" ace-window) ("o" other-frame) ("w" other-window)