3

I'm using a command that reverts a diff chunk in a buffer (linked question), however I don't know if the buffer is newly opened - or if an existing buffer is used.

While I could make a list of all buffers, then check if that buffer is in the original list, is there a better way to know if the buffer from a function is newly created or not?

Drew
  • 75,699
  • 9
  • 109
  • 225
ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • 1
    Yes, I would do what you describe. (`buffer-list` gives you a list of all buffers.) – Drew Jan 02 '20 at 05:47

1 Answers1

3

"Newly opened" can only mean relative to a state when the buffer didn't exist. Every time prior to when it came into being it didn't exist.

So yes, you need to take a snapshot of the list of existing buffers at some point prior to creating the new buffer, in order to later check whether the buffer is "new" - compared to that prior snapshot moment.

Save the value of buffer-list in a variable (e.g. let-bound), and then, later, check whether the buffer in question is in that previous snapshot. If it is, then it's not new; if it isn't, then it's new.


If you're only interested in certain kinds of buffers then you can filter buffer-list, to make your lookup quicker.

Drew
  • 75,699
  • 9
  • 109
  • 225