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

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

Re: Problems with the capitalization in Word Abbreviation Mode


From: Xah
Subject: Re: Problems with the capitalization in Word Abbreviation Mode
Date: Mon, 6 Oct 2008 21:06:14 -0700 (PDT)
User-agent: G2/1.0

another way to fix letter-case typo problem is to use one of emacs
commands to change letter case.

There are capitalize-word, downcase-word, upcase-word. Type C-h
‹function-name› to see their default keybindings.

There are also several varieties:
(upcase obj)
(upcase-word 3)
(upcase-region beg end)
(upcase-initials obj)
(upcase-initials-region beg end)
(capitalize obj)
(capitalize-word n)
(capitalize-region myStartPos myEndPos)
(downcase)
(downcase-word arg)
(downcase-region beg end)

One problem with them is that you need to move your cursor to the
beginning of the word first. For example, if your cursor is on the “A”
In “THat”, And You Call downcase-word, but it doesn't do anything
because it only start at the cursor point to end of word.

It would be nice if it'll just automatically perform the operation on
the whole word as a region.

Another problem is that it only performs one specific operation as
opposed to considering the final result. For example, if you have
“oncE upon a time ...”, and you select the whole sentence and call
upcase-initials-region, it becomes “OncE Upon A Time ...”, but the
capital E is not automatically lowered.

Also, most of these commands have a “-word” and “-region” version...
great for precision in elisp programing but not smart as user
commands. It would be nice if a region is active, then automatically
do the command on the region, else the current word. (emacs is this
way because technically, a “region” always exists. The modern concept
of text selection (with highlighting) didn't come to emacs until it
had transient-mark-mode, along with the complexity of “mark-active”.
Things would be much simpler if emacs adopted transient-mark-mode by
default.)

I've combined these into one function and i think fixed the above
usability issues:

(defun toggle-letter-case ()
  "Toggle the current word or region's letter case.
Toggles from 3 cases: upper case, lower case, title case,
in that order.
Title case means upcase first letter of each word."
(interactive)

(save-excursion
(let (pt pos1 pos2 cap1p cap2p (deactivate-mark nil) (case-fold-search
nil))
  (setq pt (point))
  (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))))

  (goto-char pos1)
  (setq cap1p (looking-at "[A-Z]"))
  (goto-char (1+ pos1))
  (setq cap2p (looking-at "[A-Z]"))

  (cond
   ((and (not cap1p) (not cap2p)) (upcase-initials-region pos1 pos2))
   ((and cap1p (not cap2p)) (upcase-region pos1 pos2) )
   ((and cap1p cap2p) (downcase-region pos1 pos2) )
   (t (downcase-region pos1 pos2) )
   )
  )
)
)

and recently assigned it a single shortcut. See
http://xahlee.org/emacs/ergonomic_emacs_keybinding.html

  Xah
∑ http://xahlee.org/

reply via email to

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