views:

115

answers:

3

In most cases I'm writing german texts. Most words start with an uppercase letter followed by lower case letters. Sometimes I'm typing too fast and also the second letter of a word is typed upper case. To work around this issue I asked myself if it is poosible to write a function which automatically changes the case of the second letter. Optional this should only happen if the third and following are in lower case. Do you know if this is possible and do you have any suggestions?

+2  A: 

Here's a command that will convert to lowercase the second letter of each word if the first letter is uppercase and all other letters in the word are lowercase:

(defun fix-double-uppercase-at-start-of-words ()
  (interactive)
  (let ((case-fold-search nil))
    (save-match-data
      (while (re-search-forward "\\b\\([[:upper:]]\\)\\([[:upper:]]\\)\\([[:lower:]]*\\)\\b" nil t)
        (replace-match (concat (match-string 1)
                               (downcase (match-string 2))
                               (match-string 3))
                       t)))))

The command will work on all words from the current cursor position to the (visible) end of the buffer.

Sean
That doesn't quite work as advertised. "TEXT" is changed to "TeXT", and "TExT" is changed to "TexT".
phils
Oops, case-fold-search needs to be nil. Fixed.
Sean
+2  A: 

Here's an 'always on' version that fixes as you type. It will let you type all uppercase words, but as soon as it detects mixed case it will capitalize.

(defun blah (s e l)
  (let ((letter (string-to-char (word-before-point))))
    (if (and (eq letter (upcase letter))
             (not (eq (char-before) (upcase (char-before)))))
        (capitalize-word -1))))
(add-to-list 'after-change-functions 'blah)
Nils
+1  A: 

You could setup a minor mode mapping all upcase characters to special input function.

See:

http://gist.github.com/516242

Jürgen Hötzel