开发者

Evaluating a random elisp function in Emacs

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 avail开发者_开发技巧able, and randomly evaluate one of them?


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*")))


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))))))


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜