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 13:50:30 -0700 (PDT)	[thread overview]
Message-ID: <44503f09-7f13-41c4-80dd-4dcda150b76d@b31g2000prb.googlegroups.com> (raw)
In-Reply-To: ef409d02-fa48-4646-aea7-9937fd230013@c22g2000prc.googlegroups.com

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= "all lower" (get this-command 'state)) (upcase-initials-
region pos1 pos2) (put this-command 'state "init caps"))
   ((string= "init caps" (get this-command 'state)) (upcase-region
pos1 pos2) (put this-command 'state "all caps"))
   ((string= "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
“(interactive ...)” 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 “(interactive ...)” clause are properly
called “commands”, meaning that it can be used BOTH by interactive
call as well in elisp code.

Going philosophical on this, i wonder if the system would better if
the presence of “(interactive ...)” is to mean that the command cannot
be allowed in elisp code. This would mean, that all elisp functions
are separated into 2 groups: those commands proper (presence of
“interactive”) and functions proper (no presence of “interactive”).
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
∑ http://xahlee.org/

  parent reply	other threads:[~2008-10-18 20:50 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
2008-10-18 20:47       ` Nikolaj Schumacher
2008-10-18 20:50       ` Xah [this message]
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=44503f09-7f13-41c4-80dd-4dcda150b76d@b31g2000prb.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.