Skip to content

Instantly share code, notes, and snippets.

@johnmastro
Created May 23, 2015 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmastro/173c6718e603ca4d27ad to your computer and use it in GitHub Desktop.
Save johnmastro/173c6718e603ca4d27ad to your computer and use it in GitHub Desktop.
cycle-case.el
(require 'pcase)
;; Based on Xah Lee's: http://ergoemacs.org/emacs/modernization_upcase-word.html
(defun cycle-case (beg end)
"Cycle the word case of the active region or word at point."
(interactive
(pcase-let ((`(,beg . ,end) (if (use-region-p)
(cons (region-beginning) (region-end))
(bounds-of-thing-at-point 'word))))
(list beg end)))
(let (deactivate-mark case-fold-search)
(unless (eq this-command last-command)
(save-excursion
(goto-char beg)
(put this-command 'state (cond ((looking-at "[[:upper:]][[:lower:]]")
'caps)
((looking-at "[[:upper:]]")
'upper)
(t
'lower)))))
(pcase (get this-command 'state)
(`lower (upcase-initials-region beg end)
(put this-command 'state 'caps))
(`caps (upcase-region beg end)
(put this-command 'state 'upper))
(_ (downcase-region beg end)
(put this-command 'state 'lower)))))
;; (global-set-key (kbd "M-C") #'cycle-case)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment