help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to convert the case for a single letter


From: Xah
Subject: Re: How to convert the case for a single letter
Date: Thu, 20 Nov 2008 03:48:25 -0800 (PST)
User-agent: G2/1.0

On Nov 20, 2:58 am, "Anand S. Dhankshirur" <a...@cdotb.ernet.in>
wrote:
> Hi,
> How do i convert the case of the letter (in the word) from lower to
> upper or vice versa.
> I know M-L and M-U do that for the word.

you might be interested in this function:

(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"))
   )
)
)

You can assign it a keyboard shortcut so you can reduce the need for
Alt+u, Alt+l, Alt+c, Ctrl+x Ctrl+u, Ctrl+x Ctrl+l to just one.

For detail, see:

• Usability Problems With Emacs's Letter-Case Commands
http://xahlee.org/emacs/modernization_upcase-word.html

  Xah
∑ http://xahlee.org/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]