Emacs Auto Load Color Theme by Time - emacs

Can I let Emacs automatically load theme ? or do certain command at customized time ? Say what I want is to M-x load-theme RET solarized-light when I am at office at 9:00am and M-x laod-theme RET solarized-dark when I am back home and continued on emacs at 8:00pm.

To expand on #Anton Kovalenko's answer, you can get the current time using the current-time-string elisp function and extracting the current time of day in hours.
If you want to write a full implementation, you could do something like (Warning, not debugged):
;; <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)
For more info on the functions used, see the following sections of the emacs manual:
Time of day
Strings
String Conversions
Idle Timers
Contains
Number Sequence

Another (very elegant) solution is theme-changer.
Given a location and day/night color themes, this file provides a change-theme function that selects the appropriate theme based on whether it is day or night. It will continue to change themes at sunrise and sunset. To install:
Set the location:
(setq calendar-location-name "Dallas, TX")
(setq calendar-latitude 32.85)
(setq calendar-longitude -96.85)
Specify the day and night themes:
(require 'theme-changer)
(change-theme 'tango 'tango-dark)
The project is hosted on Github, and can be installed through melpa.

You can use this snippet of code to do what you want.
(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)))))
Just customize the variable install-theme-loading-times as desired:
(setq install-theme-loading-times '(("9:00am" . solarized-light)
("8:00pm" . solarized-dark)))

You can start with run-with-timer function:
(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'.
Schedule a function to run every minute or so, which will check
current time and call load-theme when appropriate (don't switch
theme every minute, even if it's reloading the current theme).

Found this simple code that works for doom emacs. Put this in the config file:
(load-theme 'solarized-light t t) ;;load light theme
(run-at-time "09:00" (* 60 60 24) (lambda () (enable-theme 'solarized-light)))
(load-theme 'solarized-dark t t) ;;load dark theme
(run-at-time "20:00" (* 60 60 24) (lambda () (enable-theme 'solarized-dark)))
replace light and dark themes with your choice. time can also be changed from 9am/8pm in 24-hour format.
source and credit: https://parasurv.neocities.org/emacs/change-emacs-theme-depending-on-time.html

This implementation changes theme based on sunrise and sunset times of the latitude and longitude you provide. The only dependency is solar.el which is released with Emacs (IIRC).
(I think the code can probably be shorter here.)
;; 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))

Related

Display the binary version of hex value in status bar

I am doing a lot of embedded C programming right now, which means that I am writing things like this all the time:
(ioe_extra_A & 0xE7)
It would be super useful, if when put my cursor on the 0xE7, emacs would display "0b1110 0111" in the status bar or mini-buffer, so I could check that my mask is what I meant it to be.
Typically, no matter what it is I want emacs to do, 10 minutes of Googling will turn up the answer, but for this one, I have exhausted my searching skills and still not turned up an answer.
Thanks ahead of time.
This seems to work:
(defvar my-hex-idle-timer nil)
(defun my-hex-idle-status-on ()
(interactive)
(when (timerp my-hex-idle-timer)
(cancel-timer my-hex-idle-timer))
(setq my-hex-idle-timer (run-with-idle-timer 1 t 'my-hex-idle-status)))
(defun my-hex-idle-status-off ()
(interactive)
(when (timerp my-hex-idle-timer)
(cancel-timer my-hex-idle-timer)
(setq my-hex-idle-timer nil)))
(defun int-to-binary-string (i)
"convert an integer into it's binary representation in string format
By Trey Jackson, from https://stackoverflow.com/a/20577329/."
(let ((res ""))
(while (not (= i 0))
(setq res (concat (if (= 1 (logand i 1)) "1" "0") res))
(setq i (lsh i -1)))
(if (string= res "")
(setq res "0"))
res))
(defun my-hex-idle-status ()
(let ((word (thing-at-point 'word)))
(when (string-prefix-p "0x" word)
(let ((num (ignore-errors (string-to-number (substring word 2) 16))))
(message "In binary: %s" (int-to-binary-string num))))))
Type M-x my-hex-idle-status-on to turn it on.
As noted, thanks to Trey Jackson for int-to-binary-string.

using date-time in emacs spreadsheets

I'm just starting to use ses-mode in emacs, and I plan to use it with timestamps, but I do not manage to have them parsed in a way that I can then use.
I'm taking measurements on three days of the week, so my distances between one measurement and the other is either 2 or 3 days. I chose to use ses-mode in emacs because it runs on all of my computers, including the phone.
my spreadsheet contains datestamp, conductivity, temperature, and gallon count, a couple of subsequent lines would look like this:
2014-10-03 2.95 33.4 4031070
2014-10-06 3.07 33.5 4086930
2014-10-08 2.97 33.6 4119590
I would add two more columns, the first with the difference of days between the readings, the second with the "gallon-per-day" value.
I do not manage to have the string timestamp parsed into a format where I can do computations, staying within a simple emacs spreadsheet (SES).
I've tried date-to-time, but it always returns the same value (14445 17280).
parse-time-string gives me a 9-tuple which I can't directly pass to format-time-string.
The function encode-time helps:
(let ((l (parse-time-string "2014-09-12")))
(format-time-string "%d %m %Y" (encode-time 0 0 0 (nth 3 l) (nth 4 l) (nth 5 l))))
The following version uses cl-flet to avoid doubling of code if the encoding is needed multiple times. If you need the encoding also in other functions you can use defun instead of cl-flet.
(eval-when (compile) (require 'cl)) ;; for cl-flet
(let ((A2 "2014-10-08") ;; just for testing
(A1 "2014-10-03")) ;; just for testing
(cl-flet ((encode (str)
(let ((l (parse-time-string str)))
(encode-time 0 0 0 (nth 3 l) (nth 4 l) (nth 5 l)))))
(let* ((t-prev (encode A1))
(t-this (encode A2)))
(/ (time-to-seconds (time-subtract t-this t-prev)) (* 24 60 60)))))
As a function:
(eval-when (compile) (require 'cl)) ;; for cl-flet
(defun day-diff (date1 date2)
"Calculate the difference of dates in days between DATE1-STR and DATE2-STR."
(interactive "sDate1:\nsDate2:")
(cl-flet ((encode (str)
(let ((l (parse-time-string str)))
(encode-time 0 0 0 (nth 3 l) (nth 4 l) (nth 5 l)))))
(setq date1 (encode date1)
date2 (encode date2))
(let ((ret (/ (time-to-seconds (time-subtract date1 date2)) (* 24 60 60))))
(when (called-interactively-p 'any)
(message "Day difference: %s" ret))
ret)))
(put 'day-diff 'safe-function t)
An alternative using calc would be:
(require 'calc)
(defun day-diff (date1 date2)
"Calculate the difference of dates in days between DATE1-STR and DATE2-STR."
(interactive "sDate1:\nsDate2:")
(let ((ret (string-to-number (calc-eval (format "<%s>-<%s>" date1 date2)))))
(when (called-interactively-p 'any)
(message "Day difference: %s" ret))
ret))
If you omit the nice-to-have features this becomes almost a simple cell formula: (string-to-number (calc-eval (format "<%s>-<%s>" A1 A2))).
If you want to save the stuff in the spreadsheet you can put the defun in table cell A1. A more simple example:
(progn (defun day-diff (date1 date2) (string-to-number (calc-eval (format "<%s>-<%s>" date1 date2)))) (put 'day 'safe-function t) "Actual header")
To have a more convenient editing possibility you can switch to M-x lisp-mode.
There you find
^L
(ses-cell A1 "Actual Header" (progn (defun day-diff (date1 date2) (string-to-number (calc-eval (format "<%s>-<%s>" date1 date2)))) (put 'day 'safe-function t) "Actual header") nil nil)
which you can edit. But do not insert linebreaks! ses identifies cell-positions with line numbers in that file!
Another nice alternative is to put the definition of your function into the file-local variable list.
Switch to lisp-interaction mode by M-x lisp-interaction-mode.
Go to the end of the file. There you find the lines:
;; Local Variables:
;; mode: ses
;; End:
Add your function definition as eval to this list:
;; Local Variables:
;; mode: ses
;; eval:
;; (progn
;; (defun day-diff (date1 date2)
;; (string-to-number (calc-eval (format "<%s>-<%s>" date1 date2))))
;; (put 'day-diff 'safe-function t))
;; End:
You can add the progn without the comment characters ;. In this case even indentation works. Afterwards you can call comment-region for the progn.
You can save the file and run M-x normal-mode. Afterwards the function is defined and you can use it in the spreadsheet.

Subsequently running the same function, yielding different results

I feel a bit silly for asking this question, but I feel like my code is as inefficient as it can be. I think I do not have the logic going on too well here.
Basically, I would like to have some different things happen on subsequently running the same commands.
My idea was to have a (cond ), in which for each case I have a test whether the command used before is the same AND the value of a variable which is set according to how many times it was pressed.
I also feel like I am not getting the title/tags correctly in this case, so feel free to edit.
((and (eq last-repeatable-command 'thecommand)
(= varcounter 1))
(message "second time called")
(setq varcounter 2))
When it is pressed again, the next clause would fire.
While the code below works, I believe this could be done way more efficiently, and I hope someone can give directions on how to approach this problem.
Long code example:
(defun incremental-insert-o ()
(interactive)
; init if not bound
(when (not (boundp 'iivar)) (setq iivar 0))
(cond
((and (eq last-repeatable-command 'incremental-insert-o)
(= iivar 1))
(insert "o o ")
(setq iivar 2))
((and (eq last-repeatable-command 'incremental-insert-o)
(= iivar 2))
(insert "o o o ")
(setq iivar 3))
((and (eq last-repeatable-command 'incremental-insert-o)
(= iivar 3))
(insert "o o o "))
(t
(insert "o ")
(setq iivar 1)))
)
(global-set-key [f8] 'incremental-insert-o)
Now, you're asking for more efficient code. There are a few things you could mean by this. You could mean that you want code that executes faster. How slow is the code now? When I run it on my Emacs, it's instant. Given that this code, by definition, is called from a buttonpress, it doesn't have to be super fast. Your code is more than fast enough for its use case, so I wouldn't worry about making it any faster. It also doesn't use memory: if you call it n times, it'll still only use enough memory to store one integer: this algorithm is O(1). Sounds good to me.
You could also mean "write this in fewer lines". This will also make the code less error-prone, and easier to understand. That's certainly a reasonable goal. Your code isn't awful to begin with, so it's not a necessity, but nor is it a bad idea. There are a few modifications we could make to your function. You could drop the entire third clause of your cond, and let the (= iivar 2) case be the final one, eliminating the need to set iivar to 3 there. Well, that's better already.
But wait, the function calls (eq last-repeatable-command 'incremental-insert-o) up to three times! That's a lot. Let me try to rewrite it! First, let's start with a base function definition, with an interactive call, as you have:
(defun incremental-insert-o ()
(interactive))
Now, I'm going to restructure things from your code. First, let's see if we can keep track of iivar correctly. I'm going to rename that variable to incremental-insert-o-consecutive, for readability, and because Emacs Lisp has a single namespace, so anything else using a variable named iivar will read and write to the same place your code's looking at:
(defun incremental-insert-o ()
(interactive)
(if (eq last-repeatable-command 'incremental-insert-o)
(setq incremental-insert-o-consecutive
(1+ incremental-insert-o-consecutive))
(setq incremental-insert-o-consecutive
1)))
Is that working? I'll bind it to [F8] as you did: (global-set-key [f8] 'incremental-insert-o). Now, hit [F8] to run it, but it doesn't tell you what the return value is. Let's change the function slightly to test it:
(defun incremental-insert-o ()
(interactive)
(if (eq last-repeatable-command 'incremental-insert-o)
(setq incremental-insert-o-consecutive
(1+ incremental-insert-o-consecutive))
(setq incremental-insert-o-consecutive
1))
(message "incremental-insert-o-consecutive is currently %s" incremental-insert-o-consecutive))
Hit [F8] a few times to make sure it works, and it does! It starts at 1, increases by 1 each consecutive time it's called, and resets when you do something else. Now, we just need to print out the right message. What do we want to print? Well, the first time you call the function, print out one "o ", then the second time, print out "o o ", then the third and all other times, print "o o o ". Note that printing the second string is just printing the first string twice, and the third string is printing the first string three times:
(defun incremental-insert-o ()
(interactive)
(if (eq last-repeatable-command 'incremental-insert-o)
(setq incremental-insert-o-consecutive
(1+ incremental-insert-o-consecutive))
(setq incremental-insert-o-consecutive
1))
(dotimes (i incremental-insert-o-consecutive)
(insert "o ")))
This is almost right! It does the right thing for times 1 through 3, but doesn't cap off at inserting "o o o "; it goes on to print "o o o o ", etc. So we just need to cap off the limit of repeats at 3:
(defun incremental-insert-o ()
(interactive)
(if (eq last-repeatable-command 'incremental-insert-o)
(setq incremental-insert-o-consecutive
(1+ incremental-insert-o-consecutive))
(setq incremental-insert-o-consecutive
1))
(dotimes (i (min incremental-insert-o-consecutive
3))
(insert "o ")))
Now, this seems to do exactly what you want. Let's look at the changes from the original function. This counts the number of repeats beyond 3. But the output behavior is the same, so I don't think this matters, and it seems nicer to keep the actual count of repeats. It will break if you ever overflow the integer, but that seems unlikely. Emacs guarantees at least 536870911 as MAXINT. So let's call that a day. We did get the code shorter, and have no repeated parts. I think that makes it more readable.
Here's something I could think of, however, take it with a grain of salt, because it may be overly complex, and you don't want to bring this much complexity into what you do:
(defstruct command-state
action next-state)
(defmacro define-action-states (name condition &rest actions)
(labels ((%make-command-state
(action name)
`(make-command-state :action (lambda () ,action))))
`(let ((head ,(%make-command-state (car actions) name)))
(defvar ,name nil)
(setq ,name head)
,#(loop for action in (cdr actions)
collect
`(setf (command-state-next-state ,name)
,(%make-command-state action name)
,name (command-state-next-state ,name)))
(setf (command-state-next-state ,name) head
,name head)
(defun ,(intern (concat (symbol-name name) "-command")) ()
(when ,condition
(unwind-protect
(funcall (command-state-action ,name))
(setq ,name (command-state-next-state ,name))))))))
(define-action-states print-names (= 1 1)
(message "first state")
(message "second state")
(message "third state")
(message "fourth state"))
(print-names-command)
;; will print messages looping through them,
;; each time you call it
I've made it to use a struct, so that you could add more conditions, independent of the state itself, for example, but mostly so the names would be more self-explanatory.
Also, probably, that's not the place you should really care about efficiency - so far your fingers cannot outrun the eLisp interpreter, it's all good ;)
Here's something I did to your code to possibly improve it a bit (now the worst case scenario will only check 5 conditions instead of 6 :)
(defun smart-killer ()
(interactive)
(let* ((properties (symbol-plist 'smart-killer))
(counter (plist-get properties :counter)))
(if (region-active-p)
(kill-region (region-beginning) (region-end))
(if (eq last-repeatable-command 'smart-killer)
(if (> counter 3)
(message "Kill ring is already filled with paragraph.")
(if (> counter 2)
(progn
(yank)
(kill-new "")
(mark-paragraph -1)
(kill-region (region-beginning) (region-end)))
(if (> counter 1)
(kill-region (point) (line-beginning-position))
(kill-line))))
(when (not (looking-at "\\<\\|\\>")) (backward-word)) ; begin/end of word
(kill-word 1))
(plist-put properties :counter (mod (1+ counter) 5)))))
(put 'smart-killer :counter 0)
This is what I came up with in the end:
(defun smart-killer ()
(interactive)
(cond
; [1] If region active, kill region
((region-active-p)
(kill-region (region-beginning) (region-end)))
; [2] If this command was last called, check how many times before it ran
((eq last-repeatable-command 'smart-killer)
(cond
; [2a]
((= sm-killer 1)
(kill-line))
; [2b]
((= sm-killer 2)
(kill-region (point) (line-beginning-position)))
; [2c]
((= sm-killer 3)
(yank)
(kill-new "")
(mark-paragraph -1)
(kill-region (region-beginning) (region-end)))
; [2d]
((= sm-killer 4)
(message "Kill ring is already filled with paragraph.")))
(incf sm-killer))
; [3]
(t
(when (not (looking-at "\\<\\|\\>")) (backward-word)) ; begin/end of word
(kill-word 1)
(setq sm-killer 1)))
)

Emacs calendar: show more than 3 months?

In Emacs, when you display the calendar with M-x calendar, you get a three-month display – last month, this month, and next month – in a new window that's just 8 lines tall.
Is it possible to generate a twelve-month calendar in a full-size window?
12-MONTH CALENDAR -- SCROLLS BY MONTH (FORWARDS / BACKWARDS)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ;;;
;;; Scroll a yearly calendar by month -- in a forwards or backwards direction. ;;;
;;; ;;;
;;; To try out this example, evaluate the entire code snippet and type: ;;;
;;; ;;;
;;; M-x year-calendar ;;;
;;; ;;;
;;; To scroll forward by month, type the key: > ;;;
;;; ;;;
;;; To scroll backward by month, type the key: < ;;;
;;; ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(eval-after-load "calendar" '(progn
(define-key calendar-mode-map "<" 'lawlist-scroll-year-calendar-backward)
(define-key calendar-mode-map ">" 'lawlist-scroll-year-calendar-forward) ))
(defmacro lawlist-calendar-for-loop (var from init to final do &rest body)
"Execute a for loop.
Evaluate BODY with VAR bound to successive integers from INIT to FINAL,
inclusive. The standard macro `dotimes' is preferable in most cases."
`(let ((,var (1- ,init)))
(while (>= ,final (setq ,var (1+ ,var)))
,#body)))
(defun year-calendar (&optional month year)
"Generate a one (1) year calendar that can be scrolled by month in each direction.
This is a modification of: http://homepage3.nifty.com/oatu/emacs/calendar.html
See also: http://ivan.kanis.fr/caly.el"
(interactive)
(require 'calendar)
(let* ((current-year (number-to-string (nth 5 (decode-time (current-time)))))
(month (if month month
(string-to-number
(read-string "Please enter a month number (e.g., 1): " nil nil "1"))))
(year (if year year
(string-to-number
(read-string "Please enter a year (e.g., 2014): "
nil nil current-year)))))
(switch-to-buffer (get-buffer-create calendar-buffer))
(when (not (eq major-mode 'calendar-mode))
(calendar-mode))
(setq displayed-month month)
(setq displayed-year year)
(setq buffer-read-only nil)
(erase-buffer)
;; horizontal rows
(lawlist-calendar-for-loop j from 0 to 3 do
;; vertical columns
(lawlist-calendar-for-loop i from 0 to 2 do
(calendar-generate-month
;; month
(cond
((> (+ (* j 3) i month) 12)
(- (+ (* j 3) i month) 12))
(t
(+ (* j 3) i month)))
;; year
(cond
((> (+ (* j 3) i month) 12)
(+ year 1))
(t
year))
;; indentation / spacing between months
(+ 5 (* 25 i))))
(goto-char (point-max))
(insert (make-string (- 10 (count-lines (point-min) (point-max))) ?\n))
(widen)
(goto-char (point-max))
(narrow-to-region (point-max) (point-max)))
(widen)
(goto-char (point-min))
(setq buffer-read-only t)))
(defun lawlist-scroll-year-calendar-forward (&optional arg event)
"Scroll the yearly calendar by month in a forward direction."
(interactive (list (prefix-numeric-value current-prefix-arg)
last-nonmenu-event))
(unless arg (setq arg 1))
(save-selected-window
(if (setq event (event-start event)) (select-window (posn-window event)))
(unless (zerop arg)
(let ((month displayed-month)
(year displayed-year))
(calendar-increment-month month year arg)
(year-calendar month year)))
(goto-char (point-min))
(run-hooks 'calendar-move-hook)))
(defun lawlist-scroll-year-calendar-backward (&optional arg event)
"Scroll the yearly calendar by month in a backward direction."
(interactive (list (prefix-numeric-value current-prefix-arg)
last-nonmenu-event))
(lawlist-scroll-year-calendar-forward (- (or arg 1)) event))
There doesn't seem to be an easy way to do this. I was able to knock up the following code, which will show all twelve months, in a row, in a separate frame.
(require 'cl)
(require 'calendar)
(defun twelve-month-calendar ()
(interactive)
(let ((calendar-buffer (get-buffer-create "12-month calendar"))
(month 12)
(year 2012))
(set-buffer calendar-buffer)
(setq calendar-frame (make-frame))
(make-variable-buffer-local 'font-lock-face)
(set-face-attribute 'default calendar-frame :height 70)
(set-frame-width calendar-frame 300)
(erase-buffer)
(dotimes (i 12)
(calendar-generate-month month year 0)
(calendar-increment-month month year -1))
(calendar-mode)))
You might need to tweak it a bit, depending on your screen/font size.
I modified "12-MONTH CALENDAR -- SCROLLS BY MONTH (FORWARDS / BACKWARDS)" answer and adapted it to Emacs post 23.3 version - no calendar-for-loop macro - and changed scroll from by one month to by one year. This version show entire calendar for current year. Going backwards which < and forwards > by one year. It doesn't show on full screen, but half screen, which make it easy to use when working which vertical splits and it's more like extended version of build in calendar.
;; https://stackoverflow.com/questions/9547912/emacs-calendar-show-more-than-3-months
(defun farynaio/year-calendar (&optional year)
"Generate a one year calendar that can be scrolled by year in each direction.
This is a modification of: http://homepage3.nifty.com/oatu/emacs/calendar.html
See also: https://stackoverflow.com/questions/9547912/emacs-calendar-show-more-than-3-months"
(interactive)
(require 'calendar)
(let* (
(current-year (number-to-string (nth 5 (decode-time (current-time)))))
(month 0)
(year (if year year (string-to-number (format-time-string "%Y" (current-time))))))
(switch-to-buffer (get-buffer-create calendar-buffer))
(when (not (eq major-mode 'calendar-mode))
(calendar-mode))
(setq displayed-month month)
(setq displayed-year year)
(setq buffer-read-only nil)
(erase-buffer)
;; horizontal rows
(dotimes (j 4)
;; vertical columns
(dotimes (i 3)
(calendar-generate-month
(setq month (+ month 1))
year
;; indentation / spacing between months
(+ 5 (* 25 i))))
(goto-char (point-max))
(insert (make-string (- 10 (count-lines (point-min) (point-max))) ?\n))
(widen)
(goto-char (point-max))
(narrow-to-region (point-max) (point-max)))
(widen)
(goto-char (point-min))
(setq buffer-read-only t)))
(defun farynaio/scroll-year-calendar-forward (&optional arg event)
"Scroll the yearly calendar by year in a forward direction."
(interactive (list (prefix-numeric-value current-prefix-arg)
last-nonmenu-event))
(unless arg (setq arg 0))
(save-selected-window
(if (setq event (event-start event)) (select-window (posn-window event)))
(unless (zerop arg)
(let* (
(year (+ displayed-year arg)))
(jarfar/year-calendar year)))
(goto-char (point-min))
(run-hooks 'calendar-move-hook)))
(defun farynaio/scroll-year-calendar-backward (&optional arg event)
"Scroll the yearly calendar by year in a backward direction."
(interactive (list (prefix-numeric-value current-prefix-arg)
last-nonmenu-event))
(farynaio/scroll-year-calendar-forward (- (or arg 1)) event))
(define-key calendar-mode-map "<" 'farynaio/scroll-year-calendar-backward)
(define-key calendar-mode-map ">" 'farynaio/scroll-year-calendar-forward)
(defalias 'year-calendar 'farynaio/year-calendar)
It's not easy to do this, the code to generate calendar is:
(defun calendar-generate (month year)
"Generate a three-month Gregorian calendar centered around MONTH, YEAR."
;; A negative YEAR is interpreted as BC; -1 being 1 BC, and so on.
;; Note that while calendars for years BC could be displayed as it
;; stands, almost all other calendar functions (eg holidays) would
;; at best have unpredictable results for such dates.
(if (< (+ month (* 12 (1- year))) 2)
(error "Months before January, 1 AD cannot be displayed"))
(setq displayed-month month
displayed-year year)
(erase-buffer)
(calendar-increment-month month year -1)
(dotimes (i 3)
(calendar-generate-month month year
(+ calendar-left-margin
(* calendar-month-width i)))
(calendar-increment-month month year 1)))
Here, (dotimes (i 3) ...) generate 3 months in a row.
So if you want to generate more than 3 months in more than 1 row, you must override calendar-generate function by yourself, same as #Luke said.

Emacs - random color theme every hour?

I know that to (funcall (car (nth (random (length color-themes)) color-themes))) gives me a random color theme on every Emacs startup; but I hardly restart Emacs. How do I cycle between random color themes, say, every hour?
(defun random-color-theme ()
(interactive)
(random t)
(funcall (car (nth (random (length color-themes)) color-themes))))
(random-color-theme)
(run-with-timer 1 (* 60 60) 'random-color-theme)
Credit goes to ggole # #emacs (freenode); and aecrvol (below) for the (random t) tip.
A little improvment: adding to the function (random t),
otherwise generated sequence will be the same in each Emacs run (
from http://www.gnu.org/software/emacs/elisp/html_node/Random-Numbers.html).
(defun random-color-theme ()
(interactive)
(random t) ; randomazing
(funcall (car (nth (random (length color-themes)) color-themes))))
Here is my update:
(setq color-themes (custom-available-themes))
(defun random-color-theme ()
(interactive)
(random t)
(load-theme
(nth (random (length color-themes)) color-themes)
t))
(random-color-theme)
(run-with-timer 1 (* 60 60) 'random-color-theme)