2009-08-14

emacs upcase downcase commands problems

On Fri, Aug 14, 2009 at 8:26 AM, Stefan Monnier wrote:
...

------------------------
the code i've been using cycle the case of the first letter, on current word, or region if there's one.

here's the code i've been using for about 2 years.
Andreas Politz and Nikolaj Schumacher had helped in the code.

(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 logic on why i find this useful i wrote about here:
http://xahlee.org/emacs/modernization_upcase-word.html

Xah

No comments:

Post a Comment