From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Newsgroups: gmane.emacs.help Subject: elisp exercise: toggle-letter-case Date: Fri, 17 Oct 2008 14:02:52 -0700 (PDT) Organization: http://groups.google.com Message-ID: <420ba543-19ba-4987-9f3a-a57878777c9d@n33g2000pri.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1224279649 31760 80.91.229.12 (17 Oct 2008 21:40:49 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 17 Oct 2008 21:40:49 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Oct 17 23:41:49 2008 connect(): Connection refused Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Kqx4t-0002Om-Mb for geh-help-gnu-emacs@m.gmane.org; Fri, 17 Oct 2008 23:41:47 +0200 Original-Received: from localhost ([127.0.0.1]:37396 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Kqx3o-0006yy-QD for geh-help-gnu-emacs@m.gmane.org; Fri, 17 Oct 2008 17:40:40 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!n33g2000pri.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs,comp.lang.lisp Original-Lines: 61 Original-NNTP-Posting-Host: 24.6.185.159 Original-X-Trace: posting.google.com 1224277373 13030 127.0.0.1 (17 Oct 2008 21:02:53 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 17 Oct 2008 21:02:53 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: n33g2000pri.googlegroups.com; posting-host=24.6.185.159; posting-account=bRPKjQoAAACxZsR8_VPXCX27T2YcsyMA User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/525.18 (KHTML, like Gecko) Version/3.1.2 Safari/525.22, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:163540 comp.emacs:97201 comp.lang.lisp:254010 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:58880 Archived-At: 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 =C3=A9=C3=89 =C3=A8=C3= =88 =C3=BC=C3=9C. 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: =E2=80=A2 this command only consider English alphabets. For example, it may not work properly if you have =C3=A9=C3=89 =C3=A8=C3=88 =C3=BC=C3=9C. =E2=80=A2 It may not work when the first or second letter is a number, e.g. =E2=80=9C1time=E2=80=9D. =E2=80=A2 It may not work when you only have a single letter. e.g. =E2=80= =9CA teapot=E2=80=9D." (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 =E2=88=91 http://xahlee.org/ =E2=98=84