I am using centered-cursor-mode to center the cursor at 10 lines above center at all times. I am trying to have ggtags respects this setup after each tag lookup.

When I have the cursor at any point on screen doing M-x recenter takes it to 10 lines above center. However, executing recenter inside a lambda doesn't have the same effect instead takes the cursor to the center.

This is my setup:

  (use-package centered-cursor-mode
    :ensure t
    :diminish 'centered-cursor-mode
    (defcustom ccm-vpos-init '(round (window-text-height) 2)
      "This is the screen line position where the cursor initially stays."
      :group 'centered-cursor
      :tag "Vertical cursor position"
      :type '(choice (const :tag "Center" (round (window-text-height) 2))
                     (const :tag "Golden ratio" (round (* 21 (window-text-height)) 34))
                     (integer :tag "Lines from top" :value 10)
                     (const :tag "10 Lines above center" (- (round (window-text-height) 2) 10))))
    ;; where the center line starts (change with C-M-0/-/+)
    (setq ccm-vpos-init (quote (- (round (window-text-height) 2) 10)))
    (setq ccm-recenter-at-end-of-file  t    ;; recenter at end of file
          ccm-ignored-commands         '()  ;; Fixed hangs on big org files
    (define-key ggtags-mode-map (kbd "M-.")
    (lambda ()
    (call-interactively 'ggtags-find-tag-dwim)

This worked.

(add-hook 'ggtags-find-tag-hook #'ccm-position-cursor) 
  • Gold to always try to show the whole function at lookup: (add-hook 'ggtags-find-tag-hook #'reposition-window)
    – Gauthier
    Sep 2 '21 at 8:23

There is a vanilla way to achieve what centered cursor mode does, somewhat:

(setq scroll-preserve-screen-position t
      scroll-conservatively 0
      maximum-scroll-margin 0.5
      scroll-margin 99999)

Found here.

As far as I can see, the window is centered after ggtags look-up. I'm not sure how to modify it to get your offset of 10 rows, but it's a start.

The person who wrote the post above seems to be unhappy with the performance of centered cursor mode anyway. For my part, I couldn't make it work on my version of emacs, which is why I found this.

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.