[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: How to know the buffer I will return to from the minibuffer?
From: |
Drew Adams |
Subject: |
RE: How to know the buffer I will return to from the minibuffer? |
Date: |
Thu, 14 Feb 2019 15:38:18 +0000 (UTC) |
> 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))