Alexander-Miller / treemacs

GNU General Public License v3.0
2.12k stars 156 forks source link

Unexpected window layout when accidentially moving treemacs window with `windmove-states-swap-*` #1126

Closed IceAsteroid closed 1 month ago

IceAsteroid commented 1 month ago

After the swap by windmove-states-swap-right, in the treemacs window, the window layout becomes like follows, and none of the windows can be deleted by delete-window nor delete-other-windows.

Is there a way to prohibit such a swap action when in the treemacs window?

I know that commands like split-window-* work fine and treemacs would just stay fixed in the side window, but commands that swap states break this.

treemacs-swap-states-issue

IceAsteroid commented 1 month ago

The treemacs configuration:

  (defun my-setup/treemacs ()
    (with-eval-after-load 'treemacs
      (treemacs-load-all-the-icons-with-workaround-font "Hermit")
      (setq treemacs-display-in-side-window t
            treemacs-is-never-other-window t
               treemacs-position 'left
               treemacs-width 30
               treemacs-width-increment 1
               treemacs-width-is-initially-locked t
               treemacs-wide-toggle-width 40
               treemacs-is-never-other-window nil
               treemacs-no-delete-other-windows t
               treemacs-show-hidden-files t
               treemacs-missing-project-action 'ask
               treemacs-follow-after-init t
               treemacs-expand-after-init t
               treemacs-eldoc-display 'simple
               treemacs-project-follow-cleanup t
            )

      (add-hook 'treemacs-mode-hook
        ;; Unbind dangerous commands
        (lambda ()
          ;;unbind treemacs-delete-file
          (local-unset-key (kbd "d"))
          ;;unbind treemacs-move-file
          (local-unset-key (kbd "m"))
          ;;unbind treemacs-rename-file
          (local-unset-key (kbd "R"))))

      (keymap-global-set "C-x t t" 'treemacs) ;original command was other-tab-prefix
      ))
  (my-setup/treemacs)

Tested also in Emacs with no configuration with emacs -q, treemacs behaves the same

Alexander-Miller commented 1 month ago

It looks like the state-swap feature is just not compatible with side-windows in general. Luckily you can get around that by setting treemacs-display-in-side-window to nil.

IceAsteroid commented 1 month ago

Sorry, I was wanting treemacs to stay where it is, which means prohibit windmove-states-swap-* commands to move treemacs.

That would be better, which means the treemacs-display-in-side-window is still set to t and just like split-window-* commands cannot do things on treemacs.

Alexander-Miller commented 1 month ago

Try this:

(with-eval-after-load 'windmove
  (defun treemacs--windmove-swap-state-advice (original-fn &rest args)
    (unless (and treemacs-display-in-side-window
                 (treemacs-is-treemacs-window-selected?))
      (apply original-fn args)))

  (with-no-warnings
    (advice-add 'windmove-swap-states-in-direction
                :around #'treemacs--windmove-swap-state-advice)))
IceAsteroid commented 1 month ago

It works fine with the above code, thank you!

And besides this, use windmove-states-swap-* for example to left or right for a window in swap of treemacs, things will still go wrong. But this time it can swap back by using the opposite direction one of windmove-states-swap-*.

It's good already, just here to post to show it.

If there's two window opened horizontally, with treemacs opened as a side window, when a window is swap with treemacs, it becomes like in the following picture.

treemacs-swap-states-issue2 jpeg

Alexander-Miller commented 1 month ago

I've added my compatibility code to treemacs now, so you won't need to keep it in your config.

For the other issues you mention, unless you have set windmove-allow-all-windows to non-nil you can make treemacs invisible to windmove's commands by setting treemacs-is-never-other-window to t. Specifically it'll make treemacs invisible to all commands that target something like the "next windows".

IceAsteroid commented 1 month ago

Okay, thank you!