[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: elisp exercise: toggle-letter-case
From: |
Nikolaj Schumacher |
Subject: |
Re: elisp exercise: toggle-letter-case |
Date: |
Sat, 18 Oct 2008 22:47:27 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.3 (darwin) |
Xah <xahlee@gmail.com> wrote:
> Nik, your solution failed the spec! lol.
> It didn't work on single letter case, or words starting with number.
So what? It just does the same as yours in a more concise manner while
fixing the umlaut problem. I didn't know the challenge for a "better
solution" would include doing all the TODOs for you. :)
Those two special cases should be quickly handled, though.
(cond
((looking-at "\\([^[:alpha:]]*\\)[[:upper:]][[:upper:]]")
(capitalize-region (match-end 1) end))
((looking-at "[^[:alpha:]]*[[:upper:]]") (downcase-region (point) end))
(t (upcase-region (point) end)))
Although that makes it less readable...
I think I'd prefer this version:
(defun toggle-region-case (&optional beg end)
"Toggle the case of the word around or after point.
This toggles between downcased, upcased and capitalized. Optional
argument BEG and END (or an active mark) determine the region to work on
instead of the current word."
(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)
;; The following works in between words. Don't use thingatpt.
(goto-char (1- (point)))
(forward-word)
(setq end (point))
(backward-word))
(if (looking-at "\\([^[:alpha:]]*\\)[[:upper:]]\\([[:upper:]]\\)?")
(if (match-end 2)
(capitalize-region (match-end 1) end)
(downcase-region (point) end))
(upcase-region (point) end))
(goto-char pt)))
The semantics on regions remain a little vague, too, since only the
first word is looked at. That doesn't allow for a distinction between:
"A WORD" and "A Word"
Solving that problem properly would make the code a lot more complex.
regards,
Nikolaj Schumacher