Elisp Snippets

Some useful Elisp snippets.

Random number

Generate a random number between min and  max positive integers. 

Note: We are adding 1 to max to make it an inclusive range.

(defun random-num (max &optional min)
  (let* ((num (random (1+ max))))
    (if min (max min num) num)))
 
 

Random color


 

(defun random-color-rgb ()
  (list (random 256) (random 256) (random 256)))

(defun random-color-html ()
  (apply #'format "#%02x%02x%02x" (random-color-rgb)))
 

Random face color

Generate a random valid color for a font face.

(defun random-color-face ()
  (let* ((colors (defined-colors))
     (n (length colors)))
    (nth (random n) colors)))
 
 

HTML Text

HTML uses HTML entities for special characters. e.g. & entity for & (ampersand).

;; Buffer text to HTML Text
(xml-escape-string STRING)
 
;; HTML Text to buffer text
(xml-substitute-special STRING)
 
 

 

Comments

Popular posts from this blog

GNU Emacs as a Comic Book Reader

Data Visualization with GNU Emacs

Tinylisp and Multi-threaded Emacs