unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: martin rudalics <rudalics@gmx.at>
To: Augusto Stoffel <arstoffel@gmail.com>, emacs-devel@gnu.org
Cc: "Daniel Mendler" <mail@daniel-mendler.de>,
	"Omar Antolín Camarena" <omar.antolin@gmail.com>
Subject: Re: Switch buffers without modifying the buffer list ordering?
Date: Mon, 31 Jan 2022 09:42:10 +0100	[thread overview]
Message-ID: <1c2b4184-0f2a-95a7-16bd-d17f4bf6af53@gmx.at> (raw)
In-Reply-To: <874k5kncd8.fsf@gmail.com>

 > 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	other threads:[~2022-01-31  8:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-30 21:26 Switch buffers without modifying the buffer list ordering? Augusto Stoffel
2022-01-31  8:42 ` martin rudalics [this message]
2022-02-01 18:46   ` Augusto Stoffel
2022-02-02 11:24   ` Kevin Vigouroux via Emacs development discussions.
2022-02-01 14:48 ` Kevin Vigouroux via Emacs development discussions.

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1c2b4184-0f2a-95a7-16bd-d17f4bf6af53@gmx.at \
    --to=rudalics@gmx.at \
    --cc=arstoffel@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=mail@daniel-mendler.de \
    --cc=omar.antolin@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).