From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Newsgroups: gmane.emacs.help Subject: Re: elisp exercise: toggle-letter-case Date: Sat, 18 Oct 2008 13:50:30 -0700 (PDT) Organization: http://groups.google.com Message-ID: <44503f09-7f13-41c4-80dd-4dcda150b76d@b31g2000prb.googlegroups.com> 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=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1224366052 15164 80.91.229.12 (18 Oct 2008 21:40:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 18 Oct 2008 21:40:52 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Oct 18 23:41:57 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 1KrJYW-0006ET-Ux for geh-help-gnu-emacs@m.gmane.org; Sat, 18 Oct 2008 23:41:53 +0200 Original-Received: from localhost ([127.0.0.1]:46353 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KrJXR-0001c1-Oa for geh-help-gnu-emacs@m.gmane.org; Sat, 18 Oct 2008 17:40:45 -0400 Original-Path: news.stanford.edu!newsfeed.stanford.edu!postnews.google.com!b31g2000prb.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Original-Lines: 83 Original-NNTP-Posting-Host: 24.6.185.159 Original-X-Trace: posting.google.com 1224363030 28890 127.0.0.1 (18 Oct 2008 20:50:30 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Sat, 18 Oct 2008 20:50:30 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: b31g2000prb.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:163572 comp.emacs:97210 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:58914 Archived-At: Here's the improved version borrowing Andreas and Nikolaj's ideas. (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 cyclic order." (interactive) (let (pos1 pos2 (deactivate-mark nil) (case-fold-search nil)) (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)))) (when (not (eq last-command this-command)) (save-excursion (goto-char pos1) (cond ((looking-at "[[:lower:]][[:lower:]]") (put this-command 'state "all lower")) ((looking-at "[[:upper:]][[:upper:]]") (put this-command 'state "all caps") ) ((looking-at "[[:upper:]][[:lower:]]") (put this-command 'state "init caps") ) (t (put this-command 'state "all lower") ) ) ) ) (cond ((string=3D "all lower" (get this-command 'state)) (upcase-initials- region pos1 pos2) (put this-command 'state "init caps")) ((string=3D "init caps" (get this-command 'state)) (upcase-region pos1 pos2) (put this-command 'state "all caps")) ((string=3D "all caps" (get this-command 'state)) (downcase-region pos1 pos2) (put this-command 'state "all lower")) ) ) ) some notes: it doesn't use (&optional beg end) with the associated =E2=80=9C(interactive ...)=E2=80=9D code because i think when a command is = designed only for interactive use, then it makes sense to not support calling it in elisp as much as possible. As far as i know, elisp does not have a conventional mechanism to indicate that a function is ONLY for interactive use. More specifically, those with =E2=80=9C(interactive ...)=E2=80=9D clause are pro= perly called =E2=80=9Ccommands=E2=80=9D, meaning that it can be used BOTH by inte= ractive call as well in elisp code. Going philosophical on this, i wonder if the system would better if the presence of =E2=80=9C(interactive ...)=E2=80=9D is to mean that the com= mand cannot be allowed in elisp code. This would mean, that all elisp functions are separated into 2 groups: those commands proper (presence of =E2=80=9Cinteractive=E2=80=9D) and functions proper (no presence of =E2=80= =9Cinteractive=E2=80=9D). This also means that some class of functions that are good for both interactive and elisp use will now have to be separated into 2 versions (i.e. means more coding). But i'm guessing that over all this is a good thing and does not introduce much more labor. I think this separation is a good because with that there's a clear indication which function is for interactive use and which is for elisp only, and this indication is mechanical, i.e. build into the system so that calling a command in elisp program won't work. Right now, when a function is meant for interactive use only, emacs does its best to warn it in the inline doc or elisp manual. the above is just musings on the design, of course, in case someone takes me this to be some modernization of elisp. LOL. Xah =E2=88=91 http://xahlee.org/ =E2=98=84