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

darksky
  • 275
  • 1
  • 7
  • You provide no link for `uniquely`. Do you expect people who might help you to google for `projectile` and `uniquely`? – Drew Apr 01 '15 at 18:17
  • @Drew I'm not expecting anything.. Projectile: https://github.com/bbatsov/projectile/blob/42e991af5d915d4ac77e275f862c907366fc6f5d/projectile.el Uniquely: https://github.com/emacs-mirror/emacs/blob/0537943561a37b54467bec19d1b8afbeba8e1e58/lisp/uniquify.el – darksky Apr 01 '15 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. – Drew Apr 01 '15 at 18:35
  • I would recommend you try smart-mode-line. By default, it displays file paths relative to the projectile project. – Malabarba Apr 01 '15 at 21:08

1 Answers1

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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. – Dan Apr 01 '15 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.) – Drew Apr 01 '15 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! – darksky Apr 01 '15 at 18:31
  • `advice-add` is the new way to advise. But you can still use `defadvice` (which is good). – Drew Apr 01 '15 at 18:37