カーソルの色を変えるelisp

何だか面白そうだったので

違い

  • emacs上で名前がつけられている全ての色からランダムで選ぶようにした
  • run-with-idle-timerを使ってみた*1
  • blink-modeとか過去のカーソルの色を保存して戻せるようにした。

code

;;(cute-cursor t)   ;開始
;;(cute-cursor nil) ;終了

(require 'cl)
(defun cute-cursor-random-color ()
  (let* ((colors (defined-colors))
	 (c (nth (random (length colors)) colors)))
    (when (and cute-cursor-message-p (not (minibufferp))) 
      (message (format "this is '%s'" c)))
    c))

(defvar cute-cursor-message-p t 
  "if t, display color-name to minibuffer when called `cute-cursor-random-color'")

;;気分的にidle-timerで変えたいな。
(lexical-let ((interval 0.1) timer old-color olc-blink-cursor-mode-p)
  (defun cute-cursor (timer-on-p)
    "Start toggling cursor color when flag is true."
    (when timer (cancel-timer timer) (setq timer nil))
    (cond (timer-on-p
	   (setq old-color (frame-parameter (selected-frame) 'cursor-color)
		 old-blink-cursor-mode-p blink-cursor-mode
		 timer (run-with-idle-timer 
			interval t
			(lambda () (set-cursor-color (cute-cursor-random-color)))))
	   (blink-cursor-mode 0))
	  (t (set-cursor-color old-color)
	     (when old-blink-cursor-mode-p (blink-cursor-mode t))))))

*1:あんまり良くなかったかもしれない