emacs-devel
[Top][All Lists]
Advanced

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

Re: capitalize-dwim (was: Patch to change just-one-space)


From: Xah Lee
Subject: Re: capitalize-dwim (was: Patch to change just-one-space)
Date: Fri, 14 Aug 2009 08:36:00 -0700



On Fri, Aug 14, 2009 at 8:26 AM, Stefan Monnier <address@hidden> wrote:
>> case changing. Some work on word, some on region... with
>> transient-mark-mode on now, the region versions could be merged.

It's worth a try, indeed.

>> The lower case and upper case and cap first versions can also be
>> merged, i think, into one dwim version that just cycles.

I doubt this would work: I very often use M-u, or M-c repeatedly to
apply the change to a bunch of words: assuming the first word is
all-lowercase, what should your new command do on the second invocation:
apply the same change to the second word or cycle the capitalization
style on the first word?
 
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


reply via email to

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