From: Xah <xahlee@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: elisp exercise: toggle-letter-case
Date: Fri, 17 Oct 2008 14:02:52 -0700 (PDT) [thread overview]
Message-ID: <420ba543-19ba-4987-9f3a-a57878777c9d@n33g2000pri.googlegroups.com> (raw)
here's a little interesting exercise.
I'm writing a toggle-letter-case function below. However, it has some
problems. (see the doc string). After thinking on this for a while,
the problem seems a bit complex. It'll take perhaps few hours to fix
these, in particular, if it is to cover chars like éÉ èÈ üÜ. Also, it
seems a good solution will require this function to have a state, but
i'm reluctant to introduce a global variable for it.
I'm wondering, if anyone have a better solution?
(defun toggle-letter-case ()
"Toggle the letter case of current word or text selection.
Toggles from 3 cases: upper case, lower case, title case,
in that order.
Title case means upcase first letter of each word.
Todo:
• this command only consider English alphabets. For example, it may
not work properly if you have éÉ èÈ üÜ.
• It may not work when the first or second letter is a number, e.g.
“1time”.
• It may not work when you only have a single letter. e.g. “A
teapot”."
(interactive)
(save-excursion
(let (pt pos1 pos2 cap1p cap2p (deactivate-mark nil) (case-fold-search
nil)
)
(setq pt (point))
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning)
pos2 (region-end))
(setq pos1 (car (bounds-of-thing-at-point 'word))
pos2 (cdr (bounds-of-thing-at-point 'word))))
;; check 1th and 2th letters cases
(goto-char pos1)
(setq cap1p (looking-at "[A-Z]"))
(goto-char (1+ pos1))
(setq cap2p (looking-at "[A-Z]"))
(cond
((and (not cap1p) (not cap2p)) (upcase-initials-region pos1 pos2))
((and cap1p (not cap2p)) (upcase-region pos1 pos2) )
((and cap1p cap2p) (downcase-region pos1 pos2) )
(t (downcase-region pos1 pos2) )
)
)
)
)
PS the above assumes you have transient-mode on.
Xah
∑ http://xahlee.org/
☄
next reply other threads:[~2008-10-17 21:02 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-17 21:02 Xah [this message]
2008-10-17 23:29 ` elisp exercise: toggle-letter-case 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
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=420ba543-19ba-4987-9f3a-a57878777c9d@n33g2000pri.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.