Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Forked from dbrady/change_case.el
Created February 28, 2011 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimweirich/846928 to your computer and use it in GitHub Desktop.
Save jimweirich/846928 to your computer and use it in GitHub Desktop.
;; Grateful thanks are given to Brian Marick (@marick) for helping me
;; write these. I got the original idea while reading through
;; http://xahlee.org/emacs/elisp_idioms.html, but couldn't work out
;; the elisp regexes. Brian Marick (@marick) stepped in and helped. He
;; took my tortured and broken camelcase-region and turned it into
;; something that actually worked. I then created
;; camelcase-word-or-region. I then wrote the snakecase versions but I
;; see now that all I did was copy the camelcase defuns over and,
;; meaning to go back and finish them Real Soon Now, forgot all about
;; them. I've had a quick look (2011-02-27) but elisp regexes are
;; still pretty slippery to me, so I have decided to err on the side
;; of "Showing This To Jim Weirich" (beacuse he expressed interest in
;; the camelcase defun) and have just marked the offending code and
;; left it unfixed.
;;
;; Help me, Obi-Wan Weirichobi, you're my only hope!
;; ----------------------------------------------------------------------
;; camelcase-region Given a region of text in snake_case format,
;; changes it to camelCase.
(provide 'change-case)
(defun camelcase-region (start end)
"Changes region from snake_case to camelCase"
(interactive "r")
(save-restriction (narrow-to-region start end)
(goto-char (point-min))
(while (re-search-forward "_\\(.\\)" nil t)
(replace-match (upcase (match-string 1))))))
;; ----------------------------------------------------------------------
;; cadged largely from http://xahlee.org/emacs/elisp_idioms.html:
;;
(defun camelcase-word-or-region ()
"Changes word or region from snake_case to camelCase"
(interactive)
(let (pos1 pos2 bds)
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning) pos2 (region-end))
(progn
(setq bds (bounds-of-thing-at-point 'symbol))
(setq pos1 (car bds) pos2 (cdr bds))))
(camelcase-region pos1 pos2)))
;; ----------------------------------------------------------------------
;; snakecase-region
;; Given a region of text in camelCase format, changes it to snake_case.
;;
;; BUG: This is actually just a repeat of camelcase-region!
(defun snakecase-region (start end)
"Changes region from camelCase to snake_case"
(interactive "r")
(save-restriction
(let ((case-fold-search nil))
(narrow-to-region start end)
(goto-char (point-min))
(while (re-search-forward "\\([a-z]\\)\\([A-Z]\\)" nil t)
(message (match-string 1))
(replace-match (concat (match-string 1) "_" (downcase (match-string 2))))
(goto-char (point-min))))))
;; ----------------------------------------------------------------------
;; Given a region of text in camelCase format, changes it to snake_case.
(defun snakecase-word-or-region ()
"Changes word or region from camelCase to snake_case"
(interactive)
(let (pos1 pos2 bds)
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning) pos2 (region-end))
(progn
(setq bds (bounds-of-thing-at-point 'symbol))
(setq pos1 (car bds) pos2 (cdr bds))))
(snakecase-region pos1 pos2)))
;;; Suggested key bindings
;;; (global-set-key (kbd "C-c C--") 'camelcase-word-or-region)
;;; (global-set-key (kbd "C-c C-_") 'snakecase-word-or-region)
@jimweirich
Copy link
Author

CHANGES:

  • Fixed snakecase regexp
  • Rewind to begining for snakecase (to hande things like (thisIsATest)
  • added a (provide 'change-case)
  • renamed file to use dash
  • commented out keybindings (I have all my keybindings in a single file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment