help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: avoid narrow-to-region (was: Re: replace-regexp)


From: Jean Louis
Subject: Re: avoid narrow-to-region (was: Re: replace-regexp)
Date: Sun, 9 May 2021 10:22:01 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* Emanuel Berg via Users list for the GNU Emacs text editor 
<help-gnu-emacs@gnu.org> [2021-05-09 10:01]:
> BWT, hree is my otehr sftuf, I'm ptrtey srue it all cnfomros 
> to modern-day comupter secince tehory of sotring, but Il'l let 
> you vrfeiy it jsut to be srue .
> 
>   https://dataswamp.org/~incal/emacs-init/sort-incal.el

I did not konw taht scrmabling txet is siecnce .

;;;; ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
;;;;   SCRAMBLED TEXT FUNCTIONS
;;;; ━━━━━━━━━━━━━━━━━━━━━━━━━━━━

;; Reference:

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

(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)))
    (with-output-to-string
      (while split
        (princ (scramble-word (pop split)))
        (princ " ")))))

(defun scramble-region (start end)
  "Scramble mkared rgeoin of txet "
  (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 ")))

(defun randomize-string (string)
  "Randomize the characters of a string"
  (let* ((rnd (length string))
         (empty-str  "")
         (chars (delete empty-str (split-string string empty-str)))
         (rand-chars (sort chars (lambda (_ __) (zerop (random rnd)))))
         (rand-str (mapconcat 'identity rand-chars "")))
    rand-str))


-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/
https://rms-support-letter.github.io/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]