1

Simply checking the buffer-list is not enough, because an interactive command can be invoked via various ways, not just directly or via M-x. E.g. if you invoke it via a Helm-like selector package which creates various buffers for selection then the buffer list is affected.

Is there a reliable way to determine the origin buffer of a command which uses the minibuffer for input?

6
  • Doesn't most (or all) of the time the function current-buffer provide it? Commented Apr 24, 2022 at 13:45
  • @dalanicolai when you are in the minibuffer then the minibuffer is the current buffer, not the invoking buffer
    – Tom
    Commented Apr 24, 2022 at 13:58
  • Ah okay. Well I've tested it in Spacemacs (using ivy) calling the following command: (defun test (dir) (interactive "D") (print (current-buffer))), it printed the buffer from where I invoked ivy-switch-buffer, instead of the minibuffer. I remember something that helm does not use the minibuffer, which could be relevent (or not)... (I can not get the code block formatted 'correctly' here in the comments) Commented Apr 24, 2022 at 14:12
  • Your print function runs after the minibuffer was invoked. You can try the same using minibuffer-setup-hook which is called when the cursor is in the minibuffer.
    – Tom
    Commented Apr 24, 2022 at 14:13
  • 1
    Ah okay, thanks. Well, than the answer on my 'original' comment is probably NO :) Commented Apr 24, 2022 at 14:15

1 Answer 1

1

No, there's no way to know what buffer that is, in general.


If you use Icicles then you can know: it's the value of variable icicle-pre-minibuffer-buffer:

Buffer that was current before the minibuffer became active.

2
  • 1
    Someone care to explain the downvote? I believe the answer is correct: Emacs provides no way to know which buffer was current when the minibuffer was activated. (And yes, if in icicle-mode minor mode then you can know what that buffer is.)
    – Drew
    Commented Apr 24, 2022 at 21:03
  • When the action running in the minibuffer is finished and it is not an action which switches to another buffer (like other-window), I suppose it should come back to the origin buffer. So, I guess that something somewhere should know how to reach this buffer... Maybe it's too hidden in the calling stack or in C code?
    – duthen
    Commented Sep 21, 2022 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.