Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs inside terminal change cursor color dynamically

I'm trying to change the color of my cursor inside Emacs to another color dynamically.

I'm using emacs -nw for the terminal Emacs. The terminal I'm using is Rxvt-Unicode (Urxvt).

Since Emacs can't (I think) make changes to the terminal, (set-cursor-color "red") won't work. In order to change the cursor color of the terminal I can run echo -ne '\033]12;red\007'. This changes the color of the cursor to red.

What I tried to do in Emacs is to run this command inside it. M-x shell-command RET echo -ne '\033]12;red\007' RET

However, Emacs will escape the echoed string and print it at the bottom of the window, and not actually make any changes to the cursor.

I really don't know what else I can do from here, I don't want to use the Emacs GUI. Does anyone know a way around this? I want to be able to change the color of my cursor depending on my current mode, which is why it needs to be dynamic. If anyone has any other suggestions I would be happy to hear them!

like image 555
Attic Avatar asked Dec 10 '12 17:12

Attic


2 Answers

I think you want to use send-string-to-terminal.

E.g. (send-string-to-terminal "\033]12;red\007").

like image 146
Stefan Avatar answered Sep 22 '22 18:09

Stefan


This is a very hackish solution -- I hope someone will find a better one:

(suspend-emacs "echo -ne '\\033]12;red\\007'; fg\n")

It works by temporarily suspending the emacs process and stuffing commands into the underlying terminal to make the shell change the cursor color and resume emacs after that. However, this causes the screen to flicker while the emacs frame temporarily disappears.


Here is another very hackish and system-dependent solution:

(shell-command (format "echo -ne '\\033]12;red\\007' > /proc/%d/fd/1" (emacs-pid)))

It works (at least on Linux) by directly sending the ANSI escape sequence to the terminal (which is accessed through the /proc/PID pseudo-filesystem). I don't find this solution more elegant than the previous one, but at least it doesn't make the screen flicker.

like image 44
François Févotte Avatar answered Sep 20 '22 18:09

François Févotte