6

I am trying to create something like uniquely, which renames the buffer to the full path within the projectile project. I have the code working, however, I can't seem to figure out how to get it to run when a new buffer is open. It seems like Emacs does not provide any hooks for when a new buffer is opened.

What is a good way to run the code when a new buffer is opened? How does uniquely do it? I tried reading its source, but I can't seem to find where the code gets triggered to run.

Thanks

4
  • You provide no link for uniquely. Do you expect people who might help you to google for projectile and uniquely? Commented Apr 1, 2015 at 18:17
  • @Drew I'm not expecting anything.. Projectile: github.com/bbatsov/projectile/blob/… Uniquely: github.com/emacs-mirror/emacs/blob/… Commented Apr 1, 2015 at 18:30
  • Thanks. I think that might help get you more & better answers. It was just a suggestion. Sorry if it sounded like a rebuke. Commented Apr 1, 2015 at 18:35
  • I would recommend you try smart-mode-line. By default, it displays file paths relative to the projectile project. Commented Apr 1, 2015 at 21:08

1 Answer 1

7

You can advise create-file-buffer, if you care only about file buffers. Likewise, you can advise rename-buffer.

That's what uniquify does, for instance. Are you familiar with uniquify.el? Maybe it offers all that you need. See the Emacs manual, node Uniquify.

4
  • Any idea how one would do it for non-file buffers? I seem to recall seeing a S.O. thread a long while back that said it couldn't be done, but I could easily be misremembering. Commented Apr 1, 2015 at 18:16
  • 2
    @Dan: AFAIK, there is no single function that all buffer-creation goes through. But you can try advising get-buffer-create. That's written in C, however. You can certainly advise generate-new-buffer, which calls get-buffer-create. (But you might also ask yourself whether you really need this for non-file buffers.) Commented Apr 1, 2015 at 18:25
  • Yes, I am familiar I read through the source and couldn't figure it out. I never heard of advise before so I will take a look at what that means. I think this solves it. Thanks! Commented Apr 1, 2015 at 18:31
  • advice-add is the new way to advise. But you can still use defadvice (which is good). Commented Apr 1, 2015 at 18:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.