all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Xah <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: elisp exercise: toggle-letter-case
Date: Sat, 18 Oct 2008 11:53:48 -0700 (PDT)	[thread overview]
Message-ID: <ef409d02-fa48-4646-aea7-9937fd230013@c22g2000prc.googlegroups.com> (raw)
In-Reply-To: mailman.1411.1224291972.25473.help-gnu-emacs@gnu.org

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/

  parent reply	other threads:[~2008-10-18 18:53 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 [this message]
2008-10-18 20:47       ` Nikolaj Schumacher
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=ef409d02-fa48-4646-aea7-9937fd230013@c22g2000prc.googlegroups.com \
    --to=xahlee@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /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.