Switching between different buffers

I would like to have a keyboard shortcut to cycle between two most recently used buffers. After looking around $TEXMACS_PATH/progs/texmacs/menus/file-menu.scm I added the following to my-init-texmacs.scm:

(define (switch-to-recent-buffer)
    (if (< 1 (length (buffer-menu-list 2)))
        (switch-to-buffer (second (buffer-menu-list 2)))))

(delayed (:idle 1000)
    (kbd-map (:profile emacs)
        ("emacs:prefix b" (switch-to-recent-buffer))))

But while Go menu nicely switches between buffers, showing the buffer just as I left it with the cursor visible, my shortcut, in contrast, after switching always shows beginning of document with cursor retaining it’s original position but way off screen. Sometimes, it’s as if the view was simply scrolled off away from the cursor position and I can continue working just fine, but sometimes what happens is that scroll bar would indicate document is just one screen and a half long (no matter it’s actual size) and TeXmacs would simply refuse to scroll the view past this limit; to get things back to normal I have to switch away from the buffer and then return using the Go menu.

Strangely, that very same code

(switch-to-buffer (second (buffer-menu-list 2)))

works fine when invoked from scheme session or the original TeXmacs C-x b keyboard shortcut (defined as (interactive go-to-buffer) which also in the end switches buffers using switch-to-buffers).

Is this a bug, or am I missing something?

P.S. I use TeXmacs 2.1.1 on Ubuntu 20.04

I’ve tried to set up the keybinding in a scheme session and works perfectly for me. I’m not sure why give you problems or where the bug could lie (if any). Anyway for shortcuts it is always better to use something like

(delayed
  (lazy-keyboard-force)
  (kbd-map (:profile emacs)
        ("emacs:prefix b" (switch-to-recent-buffer))))

to force the evaluation of other shortcuts. It should not affect the behaviour you see, but you might try.

Thanks, that would mean there is some problem with my TeXmacs installation. Just to make sure, since I myself didn’t notice it right away, did you have longer document open in one of the buffers, and did you try to move the cursor somewhere in the middle of document and then switch back and forth between the buffers a couple of times using the shortcut? The expected behaviour is to keep coming to the same part of document, and not have the view jump to the begging of document, or have the other problems I talked about.

I did use long documents to test it indeed. Did you tried to run the scheme code in a scheme session and not in the init file? (and just with)

(kbd-map (:profile emacs)
        ("emacs:prefix b" (switch-to-recent-buffer)))

?

Yes I did, but it worked the same. Thanks, anyway.