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

consult-buffer preview: problem with tramp buffers and slow/unreachable hosts #224

Closed
jave opened this issue Feb 17, 2021 · 7 comments
Closed
Labels
question Further information is requested

Comments

@jave
Copy link

jave commented Feb 17, 2021

Edit a file using ssh on a remote host with tramp. consult-buffer works smoothly as long as the remote host is up.

If the remote host goes down, the situation changes. As soon as consult-buffer tries to preview the remote buffer emacs hangs.
This happens a lot when changing buffers.

Granted, this is also a tramp problem, but perhaps things could be done in consult buffer to alleviate the situation, by detecting that the remote host is down and not trying to preview it in that case.

@minad minad added the question Further information is requested label Feb 17, 2021
@minad
Copy link
Owner

minad commented Feb 17, 2021

Thank you for the report. It is a known problem that "problematic buffers" do not work well with Consult preview. A similar problem occurred for example with EXWM buffers (#204). The solution for EXWM buffers is to exclude those from the preview by configuring your own buffer sources, see https://github.com/minad/consult/wiki#do-not-preview-x-windows-exwm in the wiki. However in contrast to EXWM, tramp is a built-in Emacs problem, so we could feel more inclined to offer this configuration in Consult out of the box. On the other hand, if you have multiple open buffers with unreachable hosts it will lead to problems anyway, so we could also decide to not fix anything here. Another option is to disable preview for consult-buffer and use a manual preview key instead. Is this answer helpful?

@minad minad changed the title problem with consult-buffer and tramp interaction consult-buffer preview: problem with tramp buffers and slow/unreachable hosts Feb 17, 2021
@jave
Copy link
Author

jave commented Feb 18, 2021

Thanks. I'm also in contact with the tramp maintainer, who is very helpful.
I'm wondering why previewing a tramp buffer is problematic at all. If a link down situation is detected, tramp buffers should be "frozen" i think, from a user standpoint at least, until they can be "unfrozen". Tramp does not do this, but expects to be given reasonable calls from the above layers. The tramp maintainer is at least willing to consider an "freeze/unfreeze" aproach.

regarding the exwm solution, how would a similar solution look for tramp? If we have a workaround it would be easier to think about the more general solution.

@minad
Copy link
Owner

minad commented Feb 18, 2021

The tramp maintainer is at least willing to consider an "freeze/unfreeze" aproach.

That's good to hear.

regarding the exwm solution, how would a similar solution look for tramp? If we have a workaround it would be easier to think about the more general solution.

The EXWM solution filters the buffers depending on the EXWM mode. For TRAMP we could do a similar thing - e.g., filter remote buffers, such that they are never previewed. I think I would like to keep Consult as is, requiring users to configure their own buffer sources for such cases instead of adding a configuration layer on top which filters the candidates.

   (defun consult-buffer-state-no-tramp ()
     "Buffer state function that doesn't preview X buffers."
     (let ((orig-state (consult--buffer-state))
	     (filter (lambda (cand restore)
		       (if (or restore
			       (let ((buffer (get-buffer cand)))
				 (and buffer
				      (not (file-remote-p (buffer-local-value 'default-directory buffer))))))
			   cand
			 nil))))
	 (lambda (cand restore)
	   (funcall orig-state (funcall filter cand restore) restore))))


   (setq consult--source-buffer
	   (plist-put consult--source-buffer :state #'consult-buffer-state-no-tramp))

@minad
Copy link
Owner

minad commented Feb 21, 2021

I close this issue since I don't see how we can solve this problem differently than with the proposed buffer filtering solution. I added the snippet to the wiki. If some other approach becomes possible in the future we can revisit the issue, e.g., when Tramp adds some freeze/unfreeze mechanism.

@minad minad closed this as completed Feb 21, 2021
@jave
Copy link
Author

jave commented Feb 23, 2021

thanks!

@xdavidel
Copy link

The tramp maintainer is at least willing to consider an "freeze/unfreeze" aproach.

That's good to hear.

regarding the exwm solution, how would a similar solution look for tramp? If we have a workaround it would be easier to think about the more general solution.

The EXWM solution filters the buffers depending on the EXWM mode. For TRAMP we could do a similar thing - e.g., filter remote buffers, such that they are never previewed. I think I would like to keep Consult as is, requiring users to configure their own buffer sources for such cases instead of adding a configuration layer on top which filters the candidates.

   (defun consult-buffer-state-no-tramp ()
     "Buffer state function that doesn't preview X buffers."
     (let ((orig-state (consult--buffer-state))
	     (filter (lambda (cand restore)
		       (if (or restore
			       (let ((buffer (get-buffer cand)))
				 (and buffer
				      (not (file-remote-p (buffer-local-value 'default-directory buffer))))))
			   cand
			 nil))))
	 (lambda (cand restore)
	   (funcall orig-state (funcall filter cand restore) restore))))


   (setq consult--source-buffer
	   (plist-put consult--source-buffer :state #'consult-buffer-state-no-tramp))

This doesn't seem to work at all...
How about using an advice for the preview function instead?

pjones added a commit to pjones/emacsrc that referenced this issue Aug 11, 2022
… connections

If `default-directory' is a remote file name at the time
`consult-buffer' is started, tramp will try to start a new connection.
This mostly happens to me in vterm when I've used ssh to get to a
remote host, and then want to switch buffers.

Maybe related to minad/consult#224

Note: this issue with tramp doesn't happen with the built in
`switch-to-buffer' function.
@nurdann
Copy link

nurdann commented Aug 2, 2023

I had to change the function to open recently visited file without preview

(defun consult-buffer-state-no-tramp ()
  "Buffer state function that doesn't preview recentf and remote files"
  (let ((orig-state (consult--buffer-state))
	    (filter (lambda (cand restore)
		          (if (or (string-equal "return" cand)
                       restore
			              (let ((buffer (get-buffer cand)))
				            (and buffer
				                 (not (file-remote-p (buffer-local-value 'default-directory buffer))))))
			          cand
			        nil))))
	(lambda (cand restore)
	  (funcall orig-state (funcall filter cand restore) restore))))

Update: ye actually
the line with (get-buffer cand) will error out because cand is string, e.g. setup, preview, exit,.... So it'll error out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants