Evaluating the random elisp function in Emacs

So, I had fun with this website creating random themes for Emacs . I save the resulting .el files and load them when Emacs starts. Each color theme can be triggered by evaluating the elisp expression with a prefix inspiration-.

Unfortunately, I do not know elisp. Can someone help me figure out how I can write a function that looks at the prefix "inspiration" functions available and randomly evaluate one of them?

+3
source share
3 answers

I like to step up the solution to these problems gradually. If you just want to try my answer, skip to the code block defunat the end. I go to the buffer *scratch*in lisp-interaction-modeto try these snippets of code. You can enter C-jafter the expression, and Emacs will run it and enter the results into the buffer.

The aproposfunction searches for characters matching a certain pattern, including regular expressions. Thus, we can find all the characters starting with "inspiration" as follows:

(apropos "^inspiration-\*" t)

But this result has a list for each character with some other information. We can discard this and simply take the symbol name that will be first using the function first:

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

, , functionp:

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

. let let*, let* , . symbols functions.

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

lisp ( inspiration-). interactive, M-x use-random-inspiration elisp. funcall :

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

$HOME/.emacs .

: Apropos

(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*")))
+6

, . . "", ! , , , , . Inspiration, , .

, . , , random-dark random-light. 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))))))
+4
0
source

Source: https://habr.com/ru/post/1762412/


All Articles