qbi
qbi

Reputation: 2144

Emacs should set the second character of a word in lower case

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?

Upvotes: 2

Views: 602

Answers (3)

Jürgen Hötzel
Jürgen Hötzel

Reputation: 19727

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

See:

http://gist.github.com/516242

Upvotes: 2

Nils Fagerburg
Nils Fagerburg

Reputation: 660

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)

Upvotes: 3

Sean
Sean

Reputation: 29772

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.

Upvotes: 3

Related Questions