2

I have a custom emacs major-mode which I specified the background and foreground colors for. However, the default cursor color clashes with the background and you can't see it.

'set-cursor-color' changes the color for the entire emacs session which is undesirable. I tried using a mode-hook where I set the 'cursor' face locally using 'make-local-variable', that didn't work either (I saw no effect), I did see an effect if I set a font-lock face though, so I think the code is correct.

My guess is that the cursor is actually only settable as a global variable and is not evaluated per buffer. Is there a way I can set the cursor color per buffer so that it doesn't clobber the cursor color for other buffers? Or maybe is there a way where I can detect that I am 'entering' and 'leaving' the buffer and set the cursor color appropriately?

1
  • 1
    According to C-h f set-cursor-color (docstring) the color is set for each emacs frame (what's usually called a window outside emacs).
    – JeanPierre
    Commented Oct 1, 2016 at 21:04

3 Answers 3

1

I (I'm the OP) figured out an (imperfect) way to do it using buffer-list-update-hook (which happens whenever you switch buffers or windows https://www.emacswiki.org/emacs/SwitchingBuffers#toc8).

First I store the original cursor color:

(setq orig-cursor-background (face-attribute 'cursor :background))

Then whenever buffer-list-update-hook runs I just check the major-mode variable and see if it matches my major mode. If it does, then I set the cursor color to what I want using set-face-background. Otherwise I set it to orig-cursor-background.

The problem with this approach is that every buffer in the frame will also have the cursor changed until you switch buffers.

0
0

Library cursor-chg.el shows you how to do this. See change-cursor-mode.

Essentially, you can change the cursor type or color using a function on post-command-hook or on a major mode hook.

For example:

(add-hook 'post-command-hook 'my-cursor-change)

(defun my-cursor-change ()
  (set-cursor-color (if SOME-TEST "Orange" "Blue")))
1
  • set-cursor-color seems to set a frame parameter. I tried using set-cursor-color on a minor-mode hook, but it seems to result in the wrong color sometimes, and I'm curious if the setting of a frame parameter could explain this. I'm wondering if there's a buffer-local solution which might address the issue?
    – Hatshepsut
    Commented Jun 20, 2018 at 19:01
0

As to the first question ("Is there a way I can set the cursor color per buffer so that it doesn't clobber the cursor color for other buffers?"), the answer is: "No, this is not presently an available feature."

As described by @JeanPierre in the initial comment underneath the question above, cursor color is set on a per-frame basis -- i.e., not on a per-buffer basis, and not on a per-window basis. Therefore, all visible windows on the same frame will be affected by a change in cursor color made from anywhere inside said frame.

As to the second question ("Or maybe is there a way where I can detect that I am 'entering' and 'leaving' the buffer and set the cursor color appropriately?"), see the other answers in this thread.

One idea the original poster may wish to consider is experimenting with the variable cursor-in-non-selected-windows, which could perhaps be used in conjunction with the other answers to completely remove or change the type of cursor in non-selected windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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