0

(I will bold the actual questions.)

So here is an old version of the offical zone code that randomly chooses letters and randomly makes them uppercase or lowercase. I did not modify this, and it works just fine. (There is a newer version, and it somehow uses a function from a .c file. I'm avoiding that since I just want to stay in my .emacs file.)

(defun zone-pgm-putz-with-case ()
  (goto-char (point-min))
  (while (not (input-pending-p))
    (let ((np (+ 2 (random 5)))
          (pm (point-max)))
      (while (< np pm)
        (goto-char np)
        (let ((prec (preceding-char))
              (props (text-properties-at (1- (point)))))
          (insert (if (zerop (random 2))
                      (upcase prec)
                    (downcase prec)))
          (set-text-properties (1- (point)) (point) props))
        (backward-char 2)
        (delete-char 1)
        (setq np (+ np (1+ (random 5))))))
    (goto-char (point-min))
    (sit-for 0 2)))

And here is a simplified version I modified myself that does the same thing. (I do not know why it wasn't simplified in the first place. I'm still learning my way around elisp.)

(defun zone-putz-with-case ()
  (goto-char (point-min))
  (while (not (input-pending-p))
    (let ((np (+ 2 (random 5)))
          (pm (point-max)))
      (while (< np pm)
        (goto-char np)
        (let ((prec (preceding-char)))
          (delete-char -1)
          (insert (if (zerop (random 2))
                      (upcase prec)
                    (downcase prec))))
        (setq np (+ np (1+ (random 5))))))
    (goto-char (point-min))
    (sit-for 0 2)))

So I decided to replace the upcase/downcase with entirely different letters. (I have a few alphabet strings I'd like to try, hence the separate variable in the beginning.) After many variations and failures, here is my current code:

(defvar +test-alphabet+ "abcdefg")
(defun zone-putz-with-letters ()
  (setq alph +test-alphabet+)
  (goto-char (point-min))
  (while (not (input-pending-p))
    (let ((np (+ 2 (random 5)))
          (pm (point-max)))
      (while (< np pm)
        (goto-char np)
        (delete-char -1)
        (insert-char (aref alph (random (length alph))))
        (setq np (+ np (1+ (random 5))))
        ))
    (goto-char (point-min))
    (sit-for 0 2)))

What happens instead is all the text throughout the document disappears, and all the random inserted letters only appear on the top line. 1. Why is this happening, and how can I fix it?


Putting the let back and replacing the insert-char line with one of the following lines make the replacement work just fine, but I'm stuck only using uppercase versions of the existing letters:

(insert (upcase prec))

(insert (aref `[,prec ,prec ,(upcase prec) ,prec] 2))

2. So why does using prec work but nothing else I specify?


And interestingly, if I remove the let and do the following instead of using prec:

(insert (upcase (preceding-char)))

The result is disastrous. 3. Why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.