emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Switch buffers without modifying the buffer list ordering?


From: Augusto Stoffel
Subject: Re: Switch buffers without modifying the buffer list ordering?
Date: Tue, 01 Feb 2022 19:46:05 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.91 (gnu/linux)

Thanks, this essentially clarifies everything.

On Mon, 31 Jan 2022 at 09:42, martin rudalics <rudalics@gmx.at> wrote:

>> With the following code, I can verify that the NORECORD argument of
>> `switch-to-buffer' does what it promises:
>>
>> ```
>> (progn
>>    (switch-to-buffer "A")
>>    (switch-to-buffer "B" 'norecord)
>>    (car (buffer-list)))
>> ```
>>
>> Namely, this makes buffer B current, but returns #<buffer A>.
>>
>> Now, here's a catch: if next I type `M-x (car (buffer-list))',
>
> ... M-: (car (buffer-list)) I suppose ...
>
>> I get
>> #<buffer B> instead of #<buffer A>.  Why does that happen, and is there
>> a way to switch to buffer B and *really* not modify the buffer list
>> ordering?
>
> It's restoring the window configuration after reading from the
> minibuffer but before evaluating the expression you typed that gets in
> the way.  Try with emacs -Q and evaluate the forms below step by step
>
> (display-buffer "*Messages*")
>
> (defun foo ()
>   (message
>    "%s - %s" (selected-window) (car (buffer-list))))
>
> (add-hook 'buffer-list-update-hook 'foo)
>
> (progn
>   (switch-to-buffer "A")
>   (switch-to-buffer "B" 'norecord)
>   (car (buffer-list)))
>
> and now type M-: (car (buffer-list)).  This gets me in the *Messages*
> buffer
>
> #<window 3 on A> - A [3 times]
> #<buffer A>
> #<window 3 on B> - A
> #<window 4 on  *Minibuf-1*> -  *Minibuf-1* [4 times]
> #<window 3 on B> - B [2 times]
> #<buffer B>
>
> Now try again the same scenario but first evaluate
>
> (setq read-minibuffer-restore-windows nil)
>
> which gets me here the
>
> #<window 3 on A> - A [3 times]
> #<buffer A>
>
> you probably expected.
>
>> I'm asking this because of an issue with the live preview feature of the
>> Consult package.  Very succinctly, the idea here is to temporarily
>> change the current buffer during a `completing-read' call, in order to
>> display more information about the candidates.  When the
>> `completing-read' ends, we would like to restore the original order of
>> the buffer list.  There seems to be no simple way to achieve that, and
>> some kludge like the following seems necessary.  Any better suggestions?
>>
>> ```
>> (let ((buffers (buffer-list)))
>>      (unwind-protect
>>          <do the completing-read with live preview>
>>        (save-window-excursion
>>          ;; Restore the original buffer list ordering
>>          (dolist (buffer buffers)
>>            (when (buffer-live-p buffer)
>>              (bury-buffer-internal buffer))))))
>
> That's a different issue.  'buffer-list' is a function that returns a
> list from the internal C variable Vbuffer_alist - a variable, Lisp code
> is not allowed to modify directly.  In order to modify Vbuffer_alist, a
> Lisp programmer has to bury buffers in precisely the same way as you do.
>
> You could try coming up with a macro, say 'with-buffer-list-unmodified'
> (where you probably should suspend calling 'buffer-list-update-hook'
> while running it).
>
> martin



reply via email to

[Prev in Thread] Current Thread [Next in Thread]