Here's a function that will convert DOuble CApitals to Single Capitals. I'd originally suggested adding it to post-self-insert-hook
, but below is an option for a glorified minor mode so that you only add to that hook when you really want it:
(defun dcaps-to-scaps ()
"Convert word in DOuble CApitals to Single Capitals."
(interactive)
(and (= ?w (char-syntax (char-before)))
(save-excursion
(let ((end (point)))
(and (if (called-interactively-p)
(skip-syntax-backward "w")
(= -3 (skip-syntax-backward "w")))
(let (case-fold-search)
(looking-at "\\b[[:upper:]]\\{2\\}[[:lower:]]"))
(capitalize-region (point) end))))))
(add-hook 'post-self-insert-hook #'dcaps-to-scaps nil 'local)
And the minor mode definition:
(define-minor-mode dubcaps-mode
"Toggle `dubcaps-mode'. Converts words in DOuble CApitals to
Single Capitals as you type."
:init-value nil
:lighter (" DC")
(if dubcaps-mode
(add-hook 'post-self-insert-hook #'dcaps-to-scaps nil 'local)
(remove-hook 'post-self-insert-hook #'dcaps-to-scaps 'local)))
For what it's worth, using this version:
- is simple: just turn it on/off manually or in a mode hook;
- requires no changes to key bindings, so you're not losing any other functionality.
Even when you add it to post-self-insert-hook
, the overhead is almost non-existent, at least according to some simple benchmarking. On my machine, here's what I get with 10,000 repetitions each of a ludicrously simple form and the dcaps-to-scaps
function:
(benchmark-run-compiled 10000 (+ 1 1)) ; => .001 to .003 -ish
(benchmark-run-compiled 10000 (dcaps-to-scaps)) ; => .003 to .006 -ish
So, yes, it's slower than adding 1 + 1, but in absolute terms you'll never notice.