环境变暗时用暗色主题,变亮时用亮色主题:
(除了关灯,用手挡住 Macbook 上的摄像头的位置也行。)
文中用的是 run-with-timer
定期监测,感觉会拖慢 Emacs,我没试过。我自己写了个用 Emacs Subprocess + Filter 的方法,应该会好些。下面的代码仅供参考,如果你打算运行,得先把
sanityinc-tomorrow-eighties
sanityinc-tomorrow-day
改成你想要的暗色和亮色主题。
(defun auto-theme-mode-filter (_proc output)
(let ((current-light-sensor-reading (string-to-number output))
(current-theme (car custom-enabled-themes))
(dark-theme 'sanityinc-tomorrow-eighties)
(light-theme 'sanityinc-tomorrow-day))
(cond ((/= (length output) 8)) ; printf("%8lld", values[0]);
((and (< current-light-sensor-reading 100000)
(not (eq current-theme dark-theme)))
(disable-theme current-theme)
(enable-theme dark-theme))
((and (>= current-light-sensor-reading 100000)
(not (eq current-theme light-theme)))
(disable-theme current-theme)
(enable-theme light-theme)))))
(define-minor-mode auto-theme-mode
"Automatically set Emacs theme based on ambient light."
:global t
(let* ((buf " *auto-theme-mode*")
(proc (get-buffer-process buf)))
(if auto-theme-mode
(or (and proc (eq 'run (process-status proc)))
(let ((process-connection-type nil))
(set-process-filter
(start-process
"lmutracker"
buf
"/bin/sh"
"-c"
"while true; do lmutracker && sleep 1; done")
#'auto-theme-mode-filter)))
(and proc (kill-process proc)))))