0

I am trying to have a simple solution to center the cursor at all times. I found that centered-cursor-mode was too slow and found this simpler solution. The only problem is mouse scrolling no longer works unless I disable the minor/major mode I implemented to center the cursor.

I tried to advise mwheel-scroll to disable the mode but that was unsuccessful. I am not sure what I am doing wrong.

(define-minor-mode centered-point-mode
  "Always center the cursor on the screen."
  :lighter "..."
  (cond (centered-point-mode (add-hook 'post-command-hook 'line-change))
    (t (remove-hook 'post-command-hook 'line-change))))

(defun line-change ()
  (recenter))

(define-globalized-minor-mode my-global-centered-point-mode centered-point-mode
  (lambda () (centered-point-mode 1)))
(my-global-centered-point-mode 1)

(defun disable-centered
  (interactive)
  (centered-point-mode -1))

(advice-add 'mwheel-scroll :before (lambda () (interactive) (call-interactively 'disable-centered)))
  • 1
    No need for any advice-..., as far as I can see. How about putting in exceptions for line-change? E.g., (unless (eq this-command 'mwheel-scroll) (recenter)) If you want multiple exceptions, use (unless (memq this-command '(mwheel-scroll foo-command bar-command baz-command)) (recenter)) In most cases, this-command is the command of the current command loop -- however, certain hooks run after this-command becomes the last-command even though the command loop is still the same ... but, the post-command-hook is early enough in time so that this-command is still current. – lawlist Jan 5 at 7:11
  • That actually worked! Thanks. – SFbay007 Jan 5 at 7:48
1

The O.P. has indicated in a comment underneath the original question that a previous suggestion by this author resolved the issue, and this answer is a write-up of that solution.... There is no need to use any advice-.... Instead, the O.P. could use one of two potential conditions:

  1. For just one exception, modify the function line-change as follows:
(unless (eq this-command 'mwheel-scroll)
  (recenter))
  1. For multiple exceptions, modify the function line-change as follows:
(unless (memq this-command '(mwheel-scroll foo-command bar-command baz-command))
  (recenter))

In most cases, this-command is the command of the current command loop -- however, certain hooks run after this-command becomes the last-command even though the command loop is still the same ... but, the post-command-hook is early enough in time so that this-command is still current.

| improve this answer | |
  • As a follow up, now mouse scrolling works but the cursor moves around then recenters when moving with arrows. Is there a way to make mouse scrolling works while also fixing the cursor position? – SFbay007 Jan 5 at 15:55
  • Since this is the accepted answer, please edit the question and tags to no longer speak of "advice" and advising. IOW, it that's not really part of the question then please remove it. Thx. – Drew Jan 5 at 18:21
  • I would suggest that you launch a new question/thread to deal with: "[T]he cursor moves around then recenters when moving with arrows ... [i]s there a way to make mouse scrolling works while also fixing the cursor position?" It may behoove you to spend some time explaining exactly what you see and how you wish to change the behavior, and perhaps some code of what you have already tried. – lawlist Jan 5 at 18:27
  • Note that the second example in the answer above containing the memq statement is for situations where you have multiple exceptions. E.g., replace foo-command with left-char, and replace bar-command with right-char. To see which functions are assigned to certain keystrokes, type: C-h k and then perform the keyboard shortcut. – lawlist Jan 5 at 18:33
  • Drew, done. Lawlist, will write a new post and explain the new issue I am facing. – SFbay007 Jan 5 at 23:34

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.