I want to give an arbitrary command some buffer-local behavior without advising it. (I can do it by advising, but I want to also be able to do it without advising.)

E.g., just looking at some property on a command symbol, I want to then do something only in the buffer that was current when the command was invoked (however it was invoked).

Checking the property value can happen any time (and any number of times) after the command is invoked. The buffer-local behavior needs to change, to reflect the current value of the property.

(It's OK to limit this to buffers that are displayed. I tried looking at window-buffer-change-functions etc., but I didn't notice anything that might help.)

So far, I'm guessing that it's not possible from Lisp. But I'm hoping someone knows better, or at least knows for sure.

(I posed this question on [email protected], but haven't gotten any response yet.)

  • Are you saying that, at any arbitrary time subsequent to a command X being called, you want to know what the current buffer had been at the most-recent point in time that X was used?
    – phils
    Commented Nov 18, 2021 at 23:27
  • You can check this-command (or real-this-command) in pre-command-hook and add the (current-buffer) value to a property on the command symbol.
    – phils
    Commented Nov 18, 2021 at 23:33
  • Noting that there may not be a symbol for the command.
    – phils
    Commented Nov 18, 2021 at 23:39
  • The command property is checked during mode-line update, i.e., during redisplay, for this-command (and real-this-command isn't needed). No, I don't want to record the curent buffer for each invocation of each command, using pre-command-hook or otherwise. I was hoping that there was some record or access to that info in the command loop from Lisp, but I feared (and fear) there isn't.
    – Drew
    Commented Nov 19, 2021 at 0:55
  • I'm guessing you'll end up needing to use advice (only a guess though). If no one has an answer, but you think it would be of general benefit and can make a case for it, you could always try a feature request for future use.
    – phils
    Commented Nov 19, 2021 at 1:21

1 Answer 1


Store the current buffer in a dynamic variable, then check (current-buffer) against that variable. If they differ, then the buffer was changed.

However, I suggest that this is one of those X-Y questions that crop up from time to time. You should ask about what you’re really trying to implement, without constraining yourself to a narrow question about the implementation.

  • Thanks, but this doesn't answer the question. I have access only to a property on the command symbol - a command with the property gets checked (any time, e.g. during redisplay). I know no way to know what buffer was current when that command was invoked. If there's nothing anywhere that has recorded which buffer that was, with that record somehow accessible from Lisp, then there's no solution. (And no, it's not X-Y.)
    – Drew
    Commented Nov 18, 2021 at 19:08
  • If it’s not an X-Y question, then it is still quite vague. The characters have no depth, there is no pathos or tenderness, no tension at all, the plot holes are… wait, what are we talking about? Oh yea, the current buffer, did it change since you last looked? The only way to know is to stash the buffer, and compare the current buffer against the stashed one later. Stash it in a plist if you have to.
    – db48x
    Commented Nov 18, 2021 at 22:52

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.