0

I would like to jump to a buffer using rtags for eg.

Example snippet:

(defun generic-lookup () "Follow link/goto line."
  (interactive)
  (cond
    ((string= major-mode "c-mode")
      (progn
        (call-interactively 'rtags-find-symbol-at-point)
        (call-interactively 'recenter)))))

However this gives the error:

progn: ‘recenter’ing a window that does not display current-buffer.

How to recenter after calling a function that opens a new buffer?

  • In general, I resolve problems of this nature by identifying the window to be recentered and wrapping the recenter command in (with-selected-window WINDOW (recenter)). You'll probably need to M-x find-function RET rtags-find-symbol-at-point RET and modify it accordingly (e.g., by creating a new function) or use an advice-thing-a-majig. As you know, we obtain the WINDOW by using things like get-buffer-window if we know the buffer (by name or by its lisp object), but not the window ... I don't know if there is any benefit to interactively calling the recenter command, but ... – lawlist Feb 22 '18 at 3:31
  • Thanks for the info, I tried using (with-selected-window (get-buffer-window) (recenter)) - but this didn't work (interactively or not). Putting both functions inside a lambda which is called interactively also failed. – ideasman42 Feb 22 '18 at 5:26
  • get-buffer-window requires the correct argument as to the BUFFER ... either a valid buffer-name, or a valid buffer lisp object. In fact, you can even evaluate that command in the minibuffer and it will work without generating an error assuming the buffer exists: (with-selected-window (get-buffer-window BUFFER) (recenter)) If it works in the minibuffer with M-x eval-expression, then you are almost home free. There is also a second optional argument dealing with potentially ALL-FRAMES -- if that is applicable, you may wish to read the doc-string and see the available customizations. – lawlist Feb 22 '18 at 6:10
  • I don't use rtags, but I like to recenter things. The first thing to try is usually (setq next-error-recenter '(4)), which makes anything calling next-error do a recenter. Maybe rtags uses next-error, maybe not. Glancing at the source, it looks like rtags-buffer-name might be the buffer name you're after. Also, there's an rtags-switch-to-buffer-hook. – jpkotta Feb 23 '18 at 20:16
  • @jpkotta, I got rtags-switch-to-buffer-hook working, even so. This seems like the kind of think that could be done in a generic way. – ideasman42 Feb 23 '18 at 22:03

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.