unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Marcin Borkowski <mbork@mbork.pl>,
	Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Subject: RE: How to know the buffer I will return to from the minibuffer?
Date: Thu, 14 Feb 2019 15:38:18 +0000 (UTC)	[thread overview]
Message-ID: <eb02c48a-dbe2-4378-9998-4dfbac619705@default> (raw)
In-Reply-To: <87r2caonep.fsf@mbork.pl>

> I am writing an interactive command to be used while in the minibuffer.
> I need to know the buffer I was in when I issued the command that put
> me in the minibuffer (like M-: or M-!).  I tried (other-buffer
> (current-buffer)) and (last-buffer (current-buffer)), but to no avail.
> Then I re-read the docs for `other-buffer' and used this: (other-buffer
> (current-buffer) t), and it seemed to work.  The question is, how
> reliable it is.  Am I doing this correctly?

If you use Icicles then that buffer is the value of variable
`icicle-pre-minibuffer-buffer':

  Buffer that was current before the minibuffer became active.

and its window is the last-used window showing it:

(icicle-mru-window-for-buffer
  icicle-pre-minibuffer-buffer 'noMNI 0)

When minibuffer setup occurs, the variable is set using
function `icicle-last-non-minibuffer-buffer', which does this:

(let ((live-bufs  (icicle-remove-if
                    (lambda (buf) (not (buffer-live-p buf)))
                    (buffer-list))))
  (or (car bufs)  (car live-bufs)))

If you don't use Icicles you can do the same kind of thing.
Use `minibuffer-setup-hook' to set a variable to the last
live non-minibuffer buffer.

This is `icicle-mru-window-for-buffer':

(defun icicle-mru-window-for-buffer (buffer &optional
                                     minibuf all-frames)
  "Return the most recently used window for BUFFER.
Optional args MINIBUF and ALL-FRAMES are as for
`get-buffer-window-list'."
    (let* ((wins      (get-buffer-window-list
                        buffer minibuf all-frames))
           (mru-win   (car wins))
           (mru-time  (window-use-time mru-win))
           wtime)
      (unless (listp mru-time)
        (setq mru-time  (seconds-to-time mru-time)))
      (dolist (win  (cdr wins))
        (setq wtime  (window-use-time win))
        (unless (listp wtime)
          (setq wtime  (seconds-to-time wtime)))
        (unless (time-less-p wtime mru-time)
          (setq mru-time  wtime
                mru-win   win)))
      mru-win))



  parent reply	other threads:[~2019-02-14 15:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-14  7:10 How to know the buffer I will return to from the minibuffer? Marcin Borkowski
2019-02-14  8:39 ` tomas
2019-02-17  6:00   ` Marcin Borkowski
2019-02-14 15:38 ` Drew Adams [this message]
2019-02-17  5:59   ` Marcin Borkowski

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=eb02c48a-dbe2-4378-9998-4dfbac619705@default \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=mbork@mbork.pl \
    /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.
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).