Emacsにテーマを自動的にロードさせることはできますか?またはカスタマイズされた時間に特定のコマンドを実行しますか?私が欲しいのはM-x load-theme RET solarized-light
、私が午前9時にオフィスにM-x laod-theme RET solarized-dark
いるときと、家に戻って午後8時にemacsを続けているときです。
5 に答える
@Anton Kovalenkoの回答を拡張するために、current-time-string elisp関数を使用して現在の時刻を取得し、現在の時刻を時間単位で抽出できます。
完全な実装を記述したい場合は、次のようにすることができます(警告、デバッグされていません):
;; <Color theme initialization code>
(setq current-theme '(color-theme-solarized-light))
(defun synchronize-theme ()
(setq hour
(string-to-number
(substring (current-time-string) 11 13)))
(if (member hour (number-sequence 6 17))
(setq now '(color-theme-solarized-light))
(setq now '(color-theme-solarized-dark)))
(if (equal now current-theme)
nil
(setq current-theme now)
(eval now) ) ) ;; end of (defun ...
(run-with-timer 0 3600 synchronize-theme)
使用される関数の詳細については、emacsマニュアルの次のセクションを参照してください。
もう1つの(非常にエレガントな)ソリューションは、テーマチェンジャーです。
場所と昼/夜の色のテーマを指定すると、このファイルは、昼か夜かに基づいて適切なテーマを選択するテーマ変更機能を提供します。日の出と日の入りでテーマを変更し続けます。インストールするには:
場所を設定します。
(setq calendar-location-name "Dallas, TX")
(setq calendar-latitude 32.85)
(setq calendar-longitude -96.85)
昼と夜のテーマを指定します。
(require 'theme-changer)
(change-theme 'tango 'tango-dark)
プロジェクトはGithubでホストされており、melpaを介してインストールできます。
このコードスニペットを使用して、必要なことを実行できます。
(defvar install-theme-loading-times nil
"An association list of time strings and theme names.
The themes will be loaded at the specified time every day.")
(defvar install-theme-timers nil)
(defun install-theme-loading-at-times ()
"Set up theme loading according to `install-theme-loading-at-times`"
(interactive)
(dolist (timer install-theme-timers)
(cancel-timer timer))
(setq install-theme-timers nil)
(dolist (time-theme install-theme-loading-times)
(add-to-list 'install-theme-timers
(run-at-time (car time-theme) (* 60 60 24) 'load-theme (cdr time-theme)))))
install-theme-loading-times
必要に応じて変数をカスタマイズするだけです。
(setq install-theme-loading-times '(("9:00am" . solarized-light)
("8:00pm" . solarized-dark)))
あなたはrun-with-timer
機能から始めることができます:
(run-with-timer SECS REPEAT FUNCTION &rest ARGS)
Perform an action after a delay of SECS seconds.
Repeat the action every REPEAT seconds, if REPEAT is non-nil.
SECS and REPEAT may be integers or floating point numbers.
The action is to call FUNCTION with arguments ARGS.
This function returns a timer object which you can use in `cancel-timer'.
1分ごとに実行する関数をスケジュールします。これにより、現在の時刻が確認され、必要に応じて呼び出さload-theme
れます(現在のテーマをリロードしている場合でも、テーマを1分ごとに切り替えないでください)。
この実装は、指定した緯度と経度の日の出と日の入りの時刻に基づいてテーマを変更します。唯一の依存関係はsolar.el
、Emacs(IIRC)でリリースされるものです。
(コードはおそらくここでは短くなる可能性があると思います。)
;; theme changing at sunrises and sunsets according to lat and long
(require 'solar)
(defun today-date-integer (offset)
"Returns today's date in a list of integers, i.e. month, date, and year, in system time."
(let* ((date (mapcar
(lambda (pattern)
(string-to-number (format-time-string pattern)))
'("%m" "%d" "%Y"))))
(setcar
(nthcdr
1
date)
(+ offset (nth 1 date)))
date))
(defun current-time-decimal ()
(let* ((current-min-fraction (/ (string-to-number (format-time-string "%M")) 60.0))
(current-hour (string-to-number (format-time-string "%H"))))
(+ current-hour current-min-fraction)))
(defun next-alarm-time (sunrise-time sunset-time)
(let* ((current-time (current-time-decimal)))
(cond ((< current-time sunrise-time)
(- sunrise-time current-time))
((and (>= current-time sunrise-time)
(< current-time sunset-time))
(- sunset-time current-time))
((>= current-time sunset-time)
(let ((tomorrow-sunrise-time (car (car (solar-sunrise-sunset (today-date-integer 1))))))
(- (+ 24 tomorrow-sunrise-time) current-time))))))
(defun to-seconds (hour) (* hour 60 60))
(defun change-theme (light-theme dark-theme coor)
(let* ((_ (setq calendar-latitude (car coor)))
( _ (setq calendar-longitude (nth 1 coor)))
(today-date (today-date-integer 0))
(sunrise-sunset-list (solar-sunrise-sunset today-date))
(sunrise-time (car (car sunrise-sunset-list)))
(sunset-time (car (nth 1 sunrise-sunset-list)))
(current-time (current-time-decimal))
(current-theme (if (or (< current-time sunrise-time) (> current-time sunset-time))
dark-theme
light-theme))
(next-alarm-t (next-alarm-time sunrise-time sunset-time)))
(cancel-function-timers 'change-theme)
(load-theme current-theme t)
(run-at-time
(to-seconds next-alarm-t) nil 'change-theme light-theme dark-theme coor)))
(change-theme 'solarized-gruvbox-light 'solarized-gruvbox-dark '(47.6062 -122.3321))