[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: elisp exercise: toggle-letter-case
From: |
Xah |
Subject: |
Re: elisp exercise: toggle-letter-case |
Date: |
Sat, 18 Oct 2008 11:53:48 -0700 (PDT) |
User-agent: |
G2/1.0 |
On Oct 17, 6:06 pm, Nikolaj Schumacher <m...@nschum.de> wrote:
> Andreas Politz <poli...@fh-trier.de> wrote:
> > Giving the command a state makes it so much easier, because you
> > don't have to look at the text at all.
>
> I think looking at the text is actually less work. It's only three
> lines of code.
>
> (defun toggle-region-case (&optional beg end)
> (interactive (and transient-mark-mode mark-active
> (list (region-beginning)
> (region-end))))
> (let ((pt (point))
> case-fold-search
> deactivate-mark)
> (if beg
> (goto-char beg)
> (forward-word)
> (setq end (point))
> (backward-word))
> (cond
> ((looking-at "[[:upper:]][[:upper:]]") (downcase-region (point) end))
> ((looking-at "[[:upper:]]") (upcase-region (point) end))
> (t (upcase-initials-region (point) end)))
> (goto-char pt)))
>
> regards,
> Nikolaj Schumacher
Nik, your solution failed the spec! lol.
It didn't work on single letter case, or words starting with number.
Andreas's solution works. But he did use a state. Minor fixable
problem is that it didn't consider the word/region's current state, so
that sometimes pressing the key doesn't do anything until one invokes
it again.
The use of “[[:upper]]” is great, and the use of properties to keep
state (very nice touch), and the use of “interactive” with optional
args.
Thanks guys.
Xah
∑ http://xahlee.org/
☄