Emacs Lisp: Tihs is itneretsing ftuncion taht wlil scarmble the txet in scuh a manenr taht it stlil ramiens readable.

Emacs Lisp: Tihs is itneretsing ftuncion taht wlil scarmble the txet in scuh a manenr taht it stlil ramiens readable.

This interesting Emacs Lisp function in scrambling text in such a manner that it still remains readable, so called jumbled letters. Use GNU Emacs to send funny text to your friends.

A text like this one may be scrambled to text as below.

It seemed indeed as if the whole country in that direction was on fireโ€”a broad hillside set with minute tongues of flame, swaying and writhing with the gusts of the dying storm, and throwing a red reflection upon the cloud scud above. Every now and then a haze of smoke from some nearer conflagration drove across the window and hid the Martian shapes. I could not see what they were doing, nor the clear form of them, nor recognise the black objects they were busied upon. Neither could I see the nearer fire, though the reflections of it danced on the wall and ceiling of the study. A sharp, resinous tang of burning was in the air.

It smeeed indeed as if the whole cnuotry in taht direction was on fireโ€”a broad hilslide set wtih minute tongeus of flaem, swaying and wrhiting wtih the gutss of the dying sotrm, and thwirong a red refleioctn uopn the cluod sucd avboe. Every now and tehn a hzae of somke form smoe neearr cgonflaration drvoe arcoss the window and hid the Mtraian shapes. I could not see waht tehy wree doing, nor the claer from of thme, nor rcoegnise the baclk objetcs tehy wree bsueid uopn. Niether culod I see the neearr fire, though the reflieoctns of it dnaecd on the wlal and ceiilng of the stduy. A srhpa, rnesoius tnag of bninurg was in the ari.

(defun scramble-word (word)
  "Randoimze the cahcraters of a sritng but not fisrt and lsat "
  (let* ((first (substring word 0 1))
     (length (length word))
     (last (substring word (1- length) length)))
    (if (<= length 3)
    word
      (if (= length 4)
      (concat first (substring word 2 3) (substring word 1 2) last)
    (let* ((middle (substring word 1 (1- length)))
           (rnd (length middle))
           (empty-str "")
           (chars (delete empty-str (split-string middle empty-str)))
           (rand-chars (sort chars (lambda (_ __) (zerop (random rnd)))))
           (rand-str (mapconcat 'identity rand-chars ""))
           (new-word (concat first rand-str last)))
      new-word)))))

(defun scramble-string (string)
  "Returns string of scrambled words"
  (let* ((split (split-string string))
     (out (with-output-to-string
        (dolist (word split)
          (princ (scramble-word word))
          (princ " ")))))
    out))

(defun scramble-region (start end)
  "Scramble marked region of text"
  (interactive "r")
  (if (region-active-p)
    (let* ((s (buffer-substring-no-properties start end))
           (replacement (scramble-string s)))
      (delete-region start end)
      (insert replacement))
    (message "Tehre was no aivcte reigon to scmrable ")))

;; Reference:

;; Why your brain can read jumbled letters:
;; https://www.mnn.com/lifestyle/arts-culture/stories/why-your-brain-can-read-jumbled-letters

If somebody finds a way to do a loop so that 5 letter words and longer never comes out same, let me know. I wish to improve this function.

Related pages

Leave Your Comment or Contact GNU.Support

Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know


Full name:


E-mail:


Message: