;;; zalgo.el --- he comes! ;; Author: Noah Friedman ;; Created: 2011-05-04 ;; Keywords: games ;; License: public domain ;; $Id: zalgo.el,v 1.2 2013/08/03 17:22:02 friedman Exp $ ;;; Commentary: ;;; Code: (defvar zalgo-default-density 'normal) (defvar zalgo-char-types '(above below middle)) (defconst zalgo-chars '((above [ #x0300 #x0301 #x0302 #x0303 #x0304 #x0305 #x0306 #x0307 #x0308 #x0309 #x030a #x030b #x030c #x030d #x030e #x030f #x0310 #x0311 #x0312 #x0313 #x0314 #x031a #x033d #x033e #x033f #x0342 #x0343 #x0344 #x0346 #x034a #x034b #x034c #x0350 #x0351 #x0352 #x0357 #x035b #x0363 #x0364 #x0365 #x0366 #x0367 #x0368 #x0369 #x036a #x036b #x036c #x036d #x036e #x036f ]) (middle [ #x0315 #x031b #x0321 #x0322 #x0327 #x0328 #x0334 #x0335 #x0336 #x0337 #x0338 #x0340 #x0341 #x034f #x0358 #x035c #x035d #x035e #x035f #x0360 #x0361 #x0362 #x0489 ]) (below [ #x0316 #x0317 #x0318 #x0319 #x031c #x031d #x031e #x031f #x0320 #x0323 #x0324 #x0325 #x0326 #x0329 #x032a #x032b #x032c #x032d #x032e #x032f #x0330 #x0331 #x0332 #x0333 #x0339 #x033a #x033b #x033c #x0345 #x0347 #x0348 #x0349 #x034d #x034e #x0353 #x0354 #x0355 #x0356 #x0359 #x035a ]))) (defconst zalgo-char-hashtable (let ((tbl (make-hash-table))) (mapc (lambda (array) (mapc (lambda (c) (puthash c t tbl)) array)) (mapcar (lambda (elt) (nth 1 elt)) zalgo-chars)) tbl)) (defsubst zalgo-char-p (c) (gethash c zalgo-char-hashtable)) (defsubst zalgo-whitespace-p (c) (eq (char-syntax c) #x32)) (defsubst zalgo-rand-char (array) (aref array (random (length array)))) (defun zalgo-num-chars (&optional density) (unless density (setq density zalgo-default-density)) (cond ((eq density 'min) (list (random 8) (random 2) (random 8))) ((eq density 'normal) (list (+ (random 8) 1) (+ (random 3) 0) (+ (random 8) 1))) ((eq density 'max) (list (+ (random 16) 3) (+ (random 4) 1) (+ (random 16) 3))))) (defun zalgoify-region (beg end) (interactive "r") (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (while (< (point) (point-max)) (unless (or (zalgo-whitespace-p (char-after (point))) (zalgo-char-p (char-after (point)))) (forward-char 1) (let ((numchars (zalgo-num-chars)) (chartbls zalgo-chars) n kind tbl) (while numchars (setq n (car numchars) kind (caar chartbls) tbl (cadar chartbls)) (setq numchars (cdr numchars) chartbls (cdr chartbls)) (when (memq kind zalgo-char-types) (while (> n 0) (insert (zalgo-rand-char tbl)) (setq n (1- n))))))))))) ;;; zalgo.el ends here