Skip to content

Instantly share code, notes, and snippets.

@hanabokuro
Created September 20, 2013 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanabokuro/6636856 to your computer and use it in GitHub Desktop.
Save hanabokuro/6636856 to your computer and use it in GitHub Desktop.
toggle camelcase and snakecase.
(defun toggle-camelcase-and-snakecase ()
(interactive)
(let (start end bounds)
(if (and transient-mark-mode mark-active)
(progn
(setq start (mark))
(setq end (point)))
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(when bounds
(setq start (car bounds))
(setq end (cdr bounds))))
)
(when (and start end) (toggle-camelcase-and-snakecase-innter (min start end) (max start end)))
))
(defun toggle-camelcase-and-snakecase-innter (start end)
(save-excursion
(save-restriction
(setq case-fold-search nil)
(narrow-to-region start end)
(if (string-match "_" (buffer-substring start end))
(progn ;; snake case => camel case
(goto-char (point-min))
(while (re-search-forward "_\\(.\\)" nil t)
(replace-match (upcase (match-string 1)))))
(progn ;; camel case => snake case
(goto-char (point-min))
(while (re-search-forward "\\([a-z]\\)\\([A-Z]\\)" nil t)
(replace-match (concat (match-string 1) "_" (downcase (match-string 2))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment