tags:

views:

52

answers:

3

So, I've been having fun with this web site that creates random themes for Emacs. I've been saving the resultant .el files and loading them when starting Emacs. Each color theme can be started by evaluating an elisp expression prefixed with inspiration-.

Unfortunately, I don't know elisp. Can someone help me figure out how I might write a function that looks at what "inspiration-" prefixed functions are available, and randomly evaluate one of them?

+5  A: 

I like to build up a solution to these problems incrementally. If you just want to try my answer, skip to the defun block of code at the end. I go to the *scratch* buffer, in lisp-interaction-mode to try these code snippets out. You can type C-j after an expression and Emacs will run it and insert the results in the buffer.

The apropos function searches for symbols matching some pattern, including regular expressions. So we can find all symbols starting with "inspiration-" like so:

(apropos "^inspiration-\*" t)

But that result has a list for each symbol with some other information. We can discard that and just take the symbol name, which comes first, using the first function:

(mapcar #'first (apropos "^inspiration-\*" t))

Some of those aren't functions, so let's remove any that fail the functionp test:

(let ((symbols (mapcar #'first (apropos "^inspiration-\*" t))))
  (remove-if-not #'functionp symbols))

Now let's randomly choose one of those. I'm switching from let to let* because let* allows me to reference earlier definitions in the same initialization, e.g. using symbols when defining functions.

(let* ((symbols (mapcar #'first (apropos "^inspiration-\*" t)))
       (functions (remove-if-not #'functionp symbols))
       (number (random (length functions))))
  (nth number functions))

Now let's turn that into a new lisp function (and let's not have the name start with inspiration-). I'll mark it as interactive so that you can run it via M-x use-random-inspiration in addition to using it in other elisp code. The other big change is to use funcall to actually run the randomly selected function:

(defun use-random-inspiration ()
  (interactive)
  (let* ((symbols (mapcar #'first (apropos "^inspiration-\*" t)))
         (functions (remove-if-not #'functionp symbols))
         (number (random (length functions))))
    (funcall (nth number functions))))

So add that to your $HOME/.emacs file and try it out.

EDIT: Avoid the Apropos buffer popup

(defun use-random-inspiration ()
  (interactive)
  (let* ((pop-up-windows nil)
         (symbols (mapcar #'first (apropos "^inspiration-\*" t)))
         (functions (remove-if-not #'functionp symbols))
         (number (random (length functions))))
    (funcall (nth number functions)))
  (kill-buffer (get-buffer "*Apropos*")))
Harold L
Harold -- thank you. That is exactly what I needed. I found "mapatoms" and started trying to build something with this but not knowing elisp it was a slow road to pave. This is much more straightforward.
qrest
Turns out the only issue I have is that the apropos function brings up a new window of its own in the *Apropos* buffer. I don't see a way to suppress it, but can you?
qrest
Took me a while to figure out that apropos window pop-up. I've added a fix for that marked as an EDIT. Turns out it is a 2 part fix. First, we need to set the pop-up-windows variable to nil so that the screen doesn't split in half. That makes the pop-up take over the window. Then we can kill the *Apropos* buffer right away to go back to the buffer we were in.
Harold L
+4  A: 

I was working on an answer to this when Harold beat me to the punch. But, his answer got me thinking. I hadn't known about the Inspiration theme generator before and I really like the idea! So, while this isn't what you asked for, it still may be interesting to folks reading this question. It selects a random theme from the Inspiration site, downloads it into a buffer, evaluates it, and executes the resulting function after deleting the buffer.

Basically, it's random color themes on tap. I haven't figured out the random numbering scheme yet for light and dark, but if I do, this could easily be turned into a random-dark and random-light pair of functions. Which you could then trigger based on downloaded sunrise and sunset times for your lat and lon... =)

(defun random-inspiration ()
  "Downloads a random Inspiration theme and evaluates it."
  (interactive)
  (let* ((num (number-to-string (random 1000000)))
         (buffer (url-retrieve-synchronously
                  (concat "http://inspiration.sweyla.com/code/emacs/inspiration"
                          num
                          ".el"))))
    (save-excursion
      (set-buffer buffer)
      (goto-char (point-min))
      (re-search-forward "^$" nil 'move)
      (eval-region (point) (point-max))
      (kill-buffer (current-buffer))
      (funcall (intern-soft (concat "inspiration-" num))))))
R. P. Dillon
A: 

This isn't really an answer, but after finding the inspiration theme generator, I really wanted a nice way to tweak them...

So I made this... http://jasonm23.github.com/

slomojo