3

I'm trying to run a piece of code when a buffer/window becomes selected. For an example use case, suppose I'm editing a buffer, save it, and kill it.

Suppose also the buffer "below" (that appears and becomes selected after my kill operation) has a list of files or something (like dired) that should be refreshed after I did my editing.

I was hoping to find a hook like buffer-selected-hook or window-selected-hook, preferably a local buffer hook, so that each time my "dired-like buffer" gets activated, it could run a refresh function.

I looked at many dozens of hooks with apropos, but couldn't see anything close to what I seek. Is there a standard way (or possible way) to run a piece of my refresh code when a buffer/window becomes selected?

  • Have a look at the buffer-list-update-hook, which is mentioned in the doc-string of select-window. – lawlist Jul 21 '16 at 23:18
  • Thank you for pointing me directly at that hook. It was in the list that I looked at, but I did not connect the idea of monitoring all buffer list changes for just my singleton case. But sure enough, following the doc from the hook to select-window doc, I found this sentence: ` So if you think of running a function each time a window gets selected put it on buffer-list-update-hook'. Very good, I'll see how I fare... :-) – Kevin Jul 21 '16 at 23:39
  • I ended up using kill-buffer-hook, since I kill buffers fewer times than changing the buffer-list. Also, I already had a lambda function on the kill-buffer-hook, so adding another one was pretty easy. Thanks again for your pointers. Too bad Emacs doesn't have a local kill-buffer-hook -- that would be a much more targeted hook than a general kill buffer hook. – Kevin Jul 22 '16 at 0:14
  • I use a custom kill-buffer function instead of relying upon a kill-buffer-hook or a buffer-list-update-hook -- e.g., running my own custom hooks on the newly selected window that only fire completely if certain criteria is met; add/remove scroll-bars; redraw a cross-hairs based on cursor position; update the mode-line; prompt me to properly kill or save an e-mail buffer; with a universal argument delete all buffers that do not begin with a leading space; kill the *scratch* buffer and load my own .scratch file; delete certain frames; delete certain windows; exit recursive-edit, etc. – lawlist Jul 22 '16 at 0:34
  • 1
    I tried out the local argument of add-hook, and it did the job just fine, without the hook function having to watch for other buffer kills. Thanks @stefan for the idea. – Kevin Jul 24 '16 at 19:41
0

I'm posting a summary answer just to clear the question, so it has an "answer."

First, I think my original question was off target to the final answer. I wanted to notify a "parent" buffer when a child buffer that it spawned was killed, so the parent could refresh the buffer contents when it regained focus after the child (temp) buffer was killed. So my focus was on finding out how the parent buffer could figure out that it just became active.

There was not a easy way to find that out, or I suppose someone would have posted an answer.

So what really worked was having the child buffer call a defun (a hook function) that just updated the parent's contents. At first I used the global buffer-kill-hook, but that led to problems.

First, my hook function had to protect against every other killed buffer running the guts of my hook function. So I modified my hook function to look at the filename regexp of the killed buffer, so I could spot the filenames that were normally child buffers.

That was ok so long as only the parent edited those files by spawning them into a child buffer. But when I edited those special filenames through dired and killed them, my hook function tried to update a parent buffer that wasn't there.

When Stefan pointed out the "local" argument to add-hook, I used that feature to have the parent install a buffer-local hook on the child buffer, so that my hook function could only be run by a child who was spawned by the parent and who had the local buffer-kill-hook installed by the parent.

Now I can edit the special files through dired without bad effects, since the buffers don't have a local buffer-kill-hook installed by a parent, and because I'm not using the global buffer-kill-hook.

So everything is working fine now. Thanks to everyone for helping.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.