How does one trigger a function to run when a buffer of a particular name is created?

My use case is connecting to freenode with ERC for Emacs related channels. I manually call a function to create a new frame with a window layout specific to the channels I auto join, #emacs #org-mode #erc & #gnus. I'd like the function to automatically kick off after the last channel, in this case '#gnus', is created.

  • The time to do this is probably not when the buffer is created, but instead when it is displayed. You can use the display-buffer-alist and a custom function in conjunction with display-buffer to control displaying the buffers and run your own hook at the end. If you want to get really fancy, see this related thread, which has provisions for non-file-visiting buffers as well as file-visiting buffers -- How to intercept a file before it opens and decide which frame --stackoverflow.com/questions/18346785/…
    – lawlist
    Aug 13, 2016 at 17:40
  • If you really really really must run a function after a buffer is created, you can use the buffer-list-update-hook. If you have access to the C-source code, you can use M-x find-function RET get-buffer-create RET and see that the hook is run at the very end of said function. You would be better off, in my opinion, opening up the ERC library and tracking down the functions at issue and modify whatever needs to be modified, instead of playing around with the buffer-list-update-hook. You can, of course, put in conditions in your hook function to limit it from being called too often.
    – lawlist
    Aug 13, 2016 at 17:48
  • @lawlist your answer to stackoverflow.com/questions/18346785/… was the only thing I could find related, so thanks for that. I'm still unpacking it. My question to your replies is how is "display" defined in Emacs land? Is it when the buffer is displayed to me (such as when I switch to it from the buffer list) or when it is available to be displayed (such as when it becomes an entry in the buffer list)? Aug 13, 2016 at 18:54
  • 1
    There are two general approaches, both of which begin with get-buffer-create. The first approach (with-current-buffer (get-buffer-create . . .)) is to work on the buffer behind the scenes to prepare it with its contents and modes, and then display it after everything that needs to be done with it is already complete. The second approach is to display it immediately with something like (switch-to-buffer (get-buffer-create . . .)). Depending on the complexity, the user may see things happening with the second approach while the buffer is being populated -- this is less desired . . .
    – lawlist
    Aug 13, 2016 at 19:03
  • 1
    As you look through the erc.el library, you will want to pay attention to things like display-buffer, switch-to-buffer, switch-to-buffer-other-window, switch-to-buffer-other-frame, pop-to-buffer, etc. I briefly looked at the library today and saw a variable to control some aspects -- e.g., erc-join-buffer. However, the options are limited. My recommendation is to do everything behind the scenes in terms of populating the buffers with contents, and then use display-buffer with a custom function attached to the display-buffer-alist (I like to temporarily let-bind it).
    – lawlist
    Aug 13, 2016 at 19:05


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.