Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Randomly capitalise letters

29 views
Skip to first unread message

Johnny

unread,
Nov 22, 2012, 3:37:43 PM11/22/12
to help-gn...@gnu.org
Hi all,

I am looking for a way to randomly capitalise letters in a word or a
region, but haven't found anything in the manual or online (someone must
have had this odd idea before I am sure!). Any help on how to achieve
this would be great! For clarity, I'd like to AchiEVe soMeTHiNg LIke
thIs.


Thanks!

--
Johnny

Johnny

unread,
Nov 22, 2012, 3:51:46 PM11/22/12
to help-gn...@gnu.org
P.S.
I have tried 'studlify-region', but this seems to be a
deterministic function and does not set case randomly (as seen by
repeating the command on the same region).

--
Johnny

Pascal J. Bourguignon

unread,
Nov 22, 2012, 4:42:49 PM11/22/12
to
That's not a library command, that's for sure, but it's easy to write
it. You can learn how to configure emacs to do this kind of things by
reading:


An Introduction to Programming in Emacs Lisp
http://www.gnu.org/software/emacs/emacs-lisp-intro/ or M-: (info "(eintr)Top") RET
(for non programmers)

Emacs Lisp Manual
http://www.gnu.org/software/emacs/manual/elisp.html or M-: (info "(elisp)Top") RET

Emacs Manual
http://www.gnu.org/software/emacs/manual/ or M-: (info "(emacs)Top") RET


For example, you could write:

(defun capitalize-randomly (start end)
(interactive "r")
(goto-char start)
(while (< (point) end)
(let ((ch (char-after (point))))
(delete-region (point) (1+ (point)))
(insert (format "%c" (if (zerop (random 2))
(upcase ch)
(downcase ch)))))))


So with M-x capitalize-randomly RET you can get something like:

> I Am LOOKIng FOr A way TO rAnDOMLy cAPiTalISe lettErS In A woRd or A
> rEGIoN, BUT HaVen't fOuND anYTHiNG IN THe mANUal oR ONlINE (SomEoNe MuSt
> HaVe HaD tHIs OdD iDea bEFOrE I Am sure!). AnY HElp ON hOW To AcHIevE
> ThiS wOuLD be greAt! foR CLaRIty, i'd lIKE tO acHiEve sOmeTHiNg lIKE
> ThIs.


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Burton Samograd

unread,
Nov 22, 2012, 4:32:58 PM11/22/12
to help-gn...@gnu.org
Johnny <yggd...@gmx.co.uk> writes:

> Johnny <yggd...@gmx.co.uk> writes:
>
>> Hi all,
>>
>> I am looking for a way to randomly capitalise letters in a word or a
>> region, but haven't found anything in the manual or online (someone must
>> have had this odd idea before I am sure!). Any help on how to achieve
>> this would be great! For clarity, I'd like to AchiEVe soMeTHiNg LIke
>> thIs.
>>
>>
>> Thanks!
>
> P.S.
> I have tried 'studlify-region', but this seems to be a
> deterministic function and does not set case randomly (as seen by
> repeating the command on the same region).

This should work but you should (require 'cl) first:

(defun random-upcase-letters (downcase-factor)
"Randomly upper case letters in the current/next word.
Downcase factor is tied to how many letters are left
lower case; use 2 for 50/50 probabilty, 3 for 66/33, etc."
(interactive "P")
;; Move to the start of the next word
(forward-word)
(backward-word)
;; Do the upcasing
(let ((start (point))
(end (progn
(forward-word)
(point))))
(goto-char start)
(while (not (= start end))
(if (= 0 (random downcase-factor))
(upcase-region start (1+ start)))
(incf start))))

--
Burton Samograd


Johnny

unread,
Nov 22, 2012, 7:18:57 PM11/22/12
to Burton Samograd, help-gn...@gnu.org
Thanks, works great!

--
Johnny

Johnny

unread,
Nov 22, 2012, 7:20:39 PM11/22/12
to Pascal J. Bourguignon, help-gn...@gnu.org
Thanks Pascal, I will check out the links to try to learn some lisp,
it's long overdue!

All the best,
--
Johnny

Stefan Monnier

unread,
Nov 22, 2012, 9:30:59 PM11/22/12
to help-gn...@gnu.org
> (while (not (= start end))
> (if (= 0 (random downcase-factor))
> (upcase-region start (1+ start)))
> (incf start))))

You could speed it up with something like

(while (< start end)
(upcase-region start (1+ start))
(incf start (+ 1 (random downcase-factor))))


-- Stefan


Burton Samograd

unread,
Nov 23, 2012, 10:22:25 AM11/23/12
to help-gn...@gnu.org
Good idea in case you want to random-case 'War and Peace' :)

--
Burton Samograd


Sean McAfee

unread,
Nov 26, 2012, 1:13:09 PM11/26/12
to
"Pascal J. Bourguignon" <p...@informatimago.com> writes:
> (defun capitalize-randomly (start end)
> (interactive "r")
> (goto-char start)
> (while (< (point) end)
> (let ((ch (char-after (point))))
> (delete-region (point) (1+ (point)))
> (insert (format "%c" (if (zerop (random 2))
> (upcase ch)
> (downcase ch)))))))

It's worth noting that if you regularly use Emacs for producing random
numbers, you probably want to put "(random t)" in your .emacs file to
seed the random number generator. I once wrote some routines to
generate random events for an online game I was running, and it took a
little while for me to notice that the same events were regularly
occurring in the same order every morning.

Stefan Monnier

unread,
Nov 26, 2012, 9:30:44 PM11/26/12
to help-gn...@gnu.org
> It's worth noting that if you regularly use Emacs for producing random
> numbers, you probably want to put "(random t)" in your .emacs file to
> seed the random number generator.

Indeed. And it won't be needed in Emacs>24.2, luckily.


Stefan


jid...@jidanni.org

unread,
Nov 28, 2012, 10:22:06 PM11/28/12
to mon...@iro.umontreal.ca, help-gn...@gnu.org
(defun studlify-region (begin end)
"Studlify-case the region."

What kind of documentation is that?

The same meager amount as
$ apropos studly|/usr/games/studly
stUdly (6) - assOrteD teXt fIlteRs

I suppose.

0 new messages