Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer-list-update-hook misbehaves when selecting file via treemacs #1076

Open
IceAsteroid opened this issue Dec 7, 2023 · 6 comments
Open

Comments

@IceAsteroid
Copy link

IceAsteroid commented Dec 7, 2023

I have a function to change cursor color red/blue when read-only-mode is enabled/disabled in a buffer, and have it added to the hooks "buffer-list-update-hook", also a defind hook for (load-theme) and "read-only-mode-hook".

So, each time when I enter/leave read-only-mode or switch to a buffer thtat's enabled with read-only-mode or not, I'll have the cursor color changed respectively.

Also, the "read-only-mode" is added to the hooks "org-mode-hook" & "prog-mode-hook", which makes, org and source code files open with read-only-mode by default.

Now, while it works fine with (find-file)/C-x-f to open all the org/source-code files with red cursor color, but when I use treemacs to hit enter for some org/source coe files, the cursor once turns to red, but soon, it turns back to blue?? And treemacs works fine for most org file and source codef files, though.

After that file has been first time opened, when I select it again via treemacs, it works fine now even to open it again after it's closed.

Here's the config:

  ;;; Define after-load-theme-hook
  (defvar after-load-theme-hook nil
    "Hook run after a color theme is loaded using `load-theme'.")
  (defadvice load-theme (after run-after-load-theme-hook activate)
    "Run `after-load-theme-hook'."
    (run-hooks 'after-load-theme-hook))

  ;;; Toggle read-only-mode & cursor color changed
  (defvar current-cursor-color nil)
  (defun get-current-cursor-color ()
    "Return current theme's cursor color in current frame"
    (interactive)
    (face-attribute 'cursor :background nil 'custom-enabled-themes))
  (add-hook 'after-load-theme-hook (lambda ()
				     (setq current-cursor-color
					   (get-current-cursor-color))))
  (defun change-cursor-color-by-edition ()
    "Change cursor color when edition is enabled/disabled"
    (if buffer-read-only
	(set-cursor-color "#d54444")
      (set-cursor-color "#4084d3")))
  (defun toggle-edition ()
    (interactive)
    (call-interactively `read-only-mode))
  (add-hook 'org-mode-hook 'read-only-mode)
  (add-hook 'prog-mode-hook 'read-only-mode)
  (add-hook 'read-only-mode-hook 'change-cursor-color-by-edition)
  (add-hook 'after-load-theme-hook 'change-cursor-color-by-edition)
  (add-hook 'buffer-list-update-hook 'change-cursor-color-by-edition)
  (global-set-key (kbd "C-`") 'toggle-edition

A demo: Pay attention to 0:03 to 0:06 when the file was opened, the cursor changed from red to blue.

treemacs-issue2.mp4

Any ideas?

@Alexander-Miller
Copy link
Owner

Difficult to say. buffer-list-update-hook is called in a lot of places and treemacs does a lot of context switching to move around. You can try adding a call to (backtrace) in your hook to see where the calls are coming from. Since the change is delayed also check if git-mode or filewatch-mode case the problem.

If you're only setting read-only mode at the start you can also try using find-file-hook instead.

@IceAsteroid
Copy link
Author

IceAsteroid commented Dec 23, 2023

I've used post-command-hook specific for this instead, though it's not performance-wise, after trying lots of other hooks, this is the only one that works.

And thanks for the replay and patience, thank you

@IceAsteroid
Copy link
Author

IceAsteroid commented Dec 28, 2023

Problem solved!!!

I just set the LOCAL argument of add-hook to non-nil, and it worked.

Here's what I did after tons of attempts:

  ;; The LOCAL must set to non-til, otherwise opening files with
  ;; treemacs will make the cursor color double-switch back to the color
  ;; which is set to indiate in non-read-only-mode.
  (add-hook 'buffer-list-update-hook 'change-cursor-color-by-edition nil t)

Heck, I really don't know what happened, but it worked.

Could someone possibly explain this?

Edit: Not acutally, solving this problem created another problem, now, switching between two windows that two are not and are in read-only-mode, cursor color cannot change according to it...

Edit2: In addition to the above premise, adding the function to 'add-hook 'window-state-change-hook' now works for switching between windows.

(add-hook 'window-state-change-hook 'change-cursor-color-by-edition)

That's so weird, since I've used buffer-list-update-hook for this so long without problems, only with treemacs, not to mention, I've already, also added this self-defined function to find-file-hook, so it's the cause of this, if this is lacking.

@IceAsteroid
Copy link
Author

IceAsteroid commented Dec 30, 2023

It is more complex than I thought. Without adding my defined function change-cursor-color-by-edition to buffer-list-update-hook, I have to determine which parts to cause buffer & window change by other packages, and add the function to corresponding hooks.

Today, I encountered another package that caused cursor unable to switch color based on read-only-mode, unless I added the function to buffer-list-update-hook.

So, buffer-list-update-hook is a must, and cannot be avoided :(


Could you please explain how to use (backtrace) to debug it? I've searched on the internet, but no luck on how to use it.

@IceAsteroid IceAsteroid reopened this Dec 30, 2023
@Alexander-Miller
Copy link
Owner

Could you please explain how to use (backtrace) to debug it?

Just add a call to backtrace anywhere into your code, all it does is print a full stack trace that lead to it being called, so you know what caused the hook to be run.

You'll also want to get rid of it again after the first time it was triggered in the instance you want to investigate, otherwise it'll keep spamming stack traces as you're navigating around.

Copy link

stale bot commented Mar 1, 2024

This issue has been automatically marked as stale because it has not had recent activity (this bot only works as a reminder, it will not close issues).

@stale stale bot added the stale label Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants