Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 rivejä
5.1 KiB

4 vuotta sitten
  1. ;; moe-theme-switcher.el
  2. ;; Author: kuanyui (azazabc123@gmail.com)
  3. ;; Date: 2013/05/11 11:39
  4. ;;
  5. ;; This file is not a part of GNU Emacs,
  6. ;; but this file is released under GPL v3.
  7. (require 'moe-dark-theme)
  8. (require 'moe-light-theme)
  9. (require 'moe-theme)
  10. (defvar moe-theme-switch-by-sunrise-and-sunset t
  11. "Automatically switch between dark and light moe-theme.
  12. If this value is nil, moe-theme will switch at fixed time (06:00 and 18:00).
  13. If this value is t and both calendar-latitude and calendar-longitude are set
  14. properly, the switching will be triggered at the sunrise and sunset time of the
  15. local calendar.
  16. Take Keelung, Taiwan(25N,121E) for example, you can set like this:
  17. (setq calendar-latitude +25)
  18. (setq calendar-longitude +121)"
  19. )
  20. (defvar moe-theme-which-enabled nil
  21. "Variable indicate which theme (moe-dark or light) is being used.")
  22. (defun moe-load-theme (switch-to)
  23. "Avoid unnecessary load-theme and screen flashing in GUI version Emacs"
  24. (cond ((equal switch-to 'light)
  25. (if (not (equal moe-theme-which-enabled 'light))
  26. (progn (moe-light)
  27. (setq moe-theme-which-enabled 'light)))) ;[FIXME] Maybe unnecessary
  28. ((equal switch-to 'dark)
  29. (if (not (equal moe-theme-which-enabled 'dark))
  30. (progn (moe-dark)
  31. (setq moe-theme-which-enabled 'dark)))))) ;[FIXME] Maybe unnecessary
  32. (defun switch-at-fixed-time ()
  33. (let ((now (string-to-number (format-time-string "%H"))))
  34. (if (and (>= now 06) (<= now 18))
  35. (moe-load-theme 'light)
  36. (moe-load-theme 'dark))
  37. nil))
  38. ;; (Thanks for letoh!)
  39. ;; Fix strange bahavior of sunrise-sunset when buffer's width is too narrow.
  40. (defun get-sunrise-sunset-string ()
  41. "get the real result from `sunrise-sunset'"
  42. (save-window-excursion
  43. (let ((regex "[0-9]+:[0-9]+[ap]m")
  44. (s (sunrise-sunset))
  45. (buf (get-buffer "*temp*")))
  46. (unless (and (stringp s)
  47. (string-match-p regex s))
  48. (when buf
  49. (with-current-buffer buf
  50. (let* ((s1 (buffer-string))
  51. (s2 (if (string-match-p regex s1)
  52. s1 nil)))
  53. (setq s s2)
  54. (kill-buffer buf)))))
  55. s)))
  56. ;; Convert am/pm to 24hr and save to 24h/sunrise & 24h/set
  57. ;; Excute every 24 hr
  58. (defun convert-time-format-of-sunrise-and-sunset ()
  59. (let (rise_set a b c d e f)
  60. (setq rise_set (get-sunrise-sunset-string))
  61. (if (string-match "0:00 hours daylight" rise_set) ;If polar-night
  62. (progn
  63. (setq 24h/sunrise 'polar-night
  64. 24h/sunset 'polar-night))
  65. (if (string-match "24:00 hours daylight" rise_set) ;If midnight-sun
  66. (progn
  67. (setq 24h/sunrise 'midnight-sun
  68. 24h/sunset 'midnight-sun))
  69. (progn ;Convert 12hr to 24hr
  70. (string-match "\\([0-9][0-9]?\\):\\([0-9][0-9]\\)\\([ap]m\\).+\\([0-9][0-9]?\\):\\([0-9][0-9]\\)\\([ap]m\\)" rise_set)
  71. (setq a (string-to-number (match-string 1 rise_set))
  72. b (string-to-number (match-string 2 rise_set))
  73. c (match-string 3 rise_set)
  74. d (string-to-number (match-string 4 rise_set))
  75. e (string-to-number (match-string 5 rise_set))
  76. f (match-string 6 rise_set))
  77. (if (equal c "pm")
  78. (setq 24h/sunrise (list (+ 12 a) b))
  79. (setq 24h/sunrise (list a b)))
  80. (if (equal f "pm")
  81. (setq 24h/sunset (list (+ 12 d) e))
  82. (setq 24h/sunset (list d e))))))))
  83. ;; Excute every minute.
  84. (defun switch-by-locale ()
  85. (if (equal 24h/sunrise 'polar-night) ;If polar-night...moe-dark!
  86. (moe-load-theme 'dark)
  87. (if (equal 24h/sunrise 'midnight-sun) ;If midnight-sun...moe-light!
  88. (moe-load-theme 'light)
  89. (progn
  90. (let ((now (list (string-to-number (format-time-string "%H"))
  91. (string-to-number (format-time-string "%M")))))
  92. (if (and (or ;magic-logic [tm]
  93. (> (car now) (car 24h/sunrise))
  94. (and
  95. (= (car now) (car 24h/sunrise))
  96. (>= (second now) (second 24h/sunrise))))
  97. (or
  98. (< (car now) (car 24h/sunset))
  99. (and
  100. (= (car now) (car 24h/sunset))
  101. (< (second now) (second 24h/sunset)))))
  102. (moe-load-theme 'light)
  103. (moe-load-theme 'dark)
  104. ))))))
  105. (defun moe-theme-auto-switch ()
  106. "Automatically switch between dark and light moe-theme."
  107. (interactive)
  108. (if (boundp '24h/sunrise)
  109. (switch-by-locale)
  110. (switch-at-fixed-time))
  111. )
  112. (if (and
  113. (boundp 'calendar-longitude)
  114. (boundp 'calendar-latitude)
  115. (eql moe-theme-switch-by-sunrise-and-sunset t))
  116. (progn
  117. (convert-time-format-of-sunrise-and-sunset)
  118. (run-with-timer 0 (* 60 60 24) 'convert-time-format-of-sunrise-and-sunset))
  119. ()
  120. )
  121. (setq moe-timer (run-with-timer 0 (* 1 60) 'moe-theme-auto-switch))
  122. ;; [FIXME] A minor-mode to enable/disable moe-theme-switcher
  123. (defun moe-theme-switcher-disable ()
  124. (interactive)
  125. (cancel-timer moe-timer))
  126. (moe-theme-auto-switch)
  127. (provide 'moe-theme-switcher)