2

I'm trying to advise a function in emacs, but nothing happens.

(defadvice save-place-find-file-hook (after recenter activate)
  "Recenter after getting to saved place."
  (recenter))

Recentering doesn't happen, that is. If have a (message "foo") instead, foo is indeed displayed. What is it that I don't know about defadvice?

Drew
  • 29,895
  • 7
  • 74
  • 104
Daniel
  • 101
  • 4
  • The function might be called from a location that saves (and later restores) the display layout, that way your call to `recenter` don't take effect. – Lindydancer Jan 10 '13 at 09:57
  • Maybe instead try something like `(buffer-file-name)` to find out if the correct buffer is being recentered? – PascalVKooten Jan 10 '13 at 10:09
  • Also, perhaps try `(recenter-top-bottom)`, just in case? – PascalVKooten Jan 10 '13 at 10:11
  • The buffer is correct, and `(recenter-top-bottom)` does nothing as well... – Daniel Jan 10 '13 at 12:21
  • Also tried a `(flash-line-highlight)` and that worked. – Daniel Jan 10 '13 at 12:22
  • How am I calling `recenter` from `recenter`? Also, I don't seem to need to activate this advice (emacs 24.2), but doing so doesn't help either. I tried with `(message (buffer-file-name))` and it works fine. – Daniel Jan 10 '13 at 15:32

1 Answers1

3

find-file-hook is run before the buffer is displayed, so your recentering does not apply to the buffer you think it does and is undone immediately after it happened, when find-file replaces the current buffer with the new buffer in the selected window.

Stefan
  • 27,908
  • 4
  • 53
  • 82
  • Sounds like you must be right. Still, the flash-line works, but perhaps that's due to some peculiarity of how this buffer is replaced under my feet... Well, what can you do.. – Daniel Jan 11 '13 at 13:39
  • The flash-line works because it causes actual display, whereas recenter only makes changes that are made visible at the next redisplay. Try to add `(sit-for 1)` after your `(recenter)` to better understand what's going on. – Stefan Jan 11 '13 at 14:13
  • Ok, yeah, my advice function is run before the buffer comes in view, the visibly flashed line just stays around long enough to be seen once it does. So perhaps I would have to advice find-find-* somehow, or even deeper... – Daniel Jan 11 '13 at 18:55
  • 2
    I think the right way to do it is to use `find-file-hook` like you do, but in that hook you want to use something like `(run-with-timer 0 nil (lambda (buf) (dolist (win (get-buffer-window-list buf nil t)) (with-selected-window win (recenter)))) (current-buffer))`. – Stefan Jan 11 '13 at 22:40