all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nikolaj Schumacher <me@nschum.de>
To: Xah <xahlee@gmail.com>
Cc: help-gnu-emacs@gnu.org
Subject: Re: elisp exercise: toggle-letter-case
Date: Sat, 18 Oct 2008 22:47:27 +0200	[thread overview]
Message-ID: <m2abd1bpgg.fsf@nschum.de> (raw)
In-Reply-To: <ef409d02-fa48-4646-aea7-9937fd230013@c22g2000prc.googlegroups.com> (Xah's message of "Sat\, 18 Oct 2008 11\:53\:48 -0700 \(PDT\)")

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




  reply	other threads:[~2008-10-18 20:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-17 21:02 elisp exercise: toggle-letter-case Xah
2008-10-17 23:29 ` Andreas Politz
2008-10-18  1:06   ` Nikolaj Schumacher
     [not found]   ` <mailman.1411.1224291972.25473.help-gnu-emacs@gnu.org>
2008-10-18 18:53     ` Xah
2008-10-18 20:47       ` Nikolaj Schumacher [this message]
2008-10-18 20:50       ` Xah
2008-10-19 15:26         ` Nikolaj Schumacher
     [not found]         ` <mailman.1506.1224429976.25473.help-gnu-emacs@gnu.org>
2008-10-19 17:46           ` Xah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2abd1bpgg.fsf@nschum.de \
    --to=me@nschum.de \
    --cc=help-gnu-emacs@gnu.org \
    --cc=xahlee@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.