From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Nikolaj Schumacher Newsgroups: gmane.emacs.help Subject: Re: elisp exercise: toggle-letter-case Date: Sat, 18 Oct 2008 22:47:27 +0200 Message-ID: References: <420ba543-19ba-4987-9f3a-a57878777c9d@n33g2000pri.googlegroups.com> <1224286525.260548@arno.fh-trier.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1224362922 5913 80.91.229.12 (18 Oct 2008 20:48:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 18 Oct 2008 20:48:42 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Xah Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Oct 18 22:49:39 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 1KrIjK-0000w2-TN for geh-help-gnu-emacs@m.gmane.org; Sat, 18 Oct 2008 22:48:59 +0200 Original-Received: from localhost ([127.0.0.1]:38339 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KrIiF-0004XQ-P1 for geh-help-gnu-emacs@m.gmane.org; Sat, 18 Oct 2008 16:47:51 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KrIhx-0004XK-I5 for help-gnu-emacs@gnu.org; Sat, 18 Oct 2008 16:47:33 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KrIhu-0004Wt-Kj for help-gnu-emacs@gnu.org; Sat, 18 Oct 2008 16:47:31 -0400 Original-Received: from [199.232.76.173] (port=32846 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KrIhu-0004Wq-Hq for help-gnu-emacs@gnu.org; Sat, 18 Oct 2008 16:47:30 -0400 Original-Received: from dd18200.kasserver.com ([85.13.138.168]:58343) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KrIhu-0006sc-Ac for help-gnu-emacs@gnu.org; Sat, 18 Oct 2008 16:47:30 -0400 Original-Received: from thursday (BAH545e.bah.pppool.de [77.135.84.94]) by dd18200.kasserver.com (Postfix) with ESMTP id 97B411807791E; Sat, 18 Oct 2008 22:47:33 +0200 (CEST) In-Reply-To: (Xah's message of "Sat\, 18 Oct 2008 11\:53\:48 -0700 \(PDT\)") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.3 (darwin) X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) 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:58913 Archived-At: Xah 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