I want to replace words like 1st with 1^{st} and 102nd with 102^{nd} while typing in emacs. Basically I want to replace things that match this:
"\([0-9]+\)\(st\|nd\|rd\|th\)"
with this \1^{\2} as I type.
It seems like an abbrev might work for this, but it does not appear you can put a regexp as the name.
The best I have come up with is
(defun org-ordinal-expansion ()
(interactive)
(and (= ?w (char-syntax (char-before)))
(save-excursion
(and (skip-syntax-backward "w")
(let (case-fold-search)
(looking-at "\\([0-9]+\\)\\(st\\|nd\\|rd\\|th\\)"))
(replace-match "\\1^{\\2}")))))
(define-minor-mode ordinal-mode
"Toggle `ordinal-mode'. Converst 1st to 1^{st} as you type."
:init-value nil
:lighter (" om")
(if ordinal-mode
(add-hook 'post-self-insert-hook #'org-ordinal-expansion nil 'local)
(remove-hook 'post-self-insert-hook #'org-ordinal-expansion 'local)))
Is there some more elegant way to achieve this that doesn't rely on the post-self-insert-hook?
write-file-functions
so that it happens every time you save the file. At least then you'd be to use a single regexp search and replace.