Skip to content

Instantly share code, notes, and snippets.

@Philonous
Created June 4, 2014 20:14
Show Gist options
  • Select an option

  • Save Philonous/16ace93247515c3d29a8 to your computer and use it in GitHub Desktop.

Select an option

Save Philonous/16ace93247515c3d29a8 to your computer and use it in GitHub Desktop.
(defun underscore-to-camelcase ()
"Convert underscores to camelCase"
(interactive)
(destructuring-bind (beg . end) (bounds-of-thing-at-point 'word)
(goto-char beg)
(while (re-search-forward "_\\([a-z]\\)" end t)
(replace-match (upcase (match-string 1))))))
(defun camelcase-to-underscore ()
"Convert underscores to camelCase"
(interactive)
(let ((case-fold-search nil))
(destructuring-bind (beg . end) (bounds-of-thing-at-point 'word)
(goto-char beg)
(while (re-search-forward "\\([a-z0-9]\\)\\([A-Z]\\)" end t)
(replace-match (concat (match-string-no-properties 1)
"_"
(downcase (match-string-no-properties 2))))))))
(defun swap-underscore-camelcase ()
"convert udnerscores to camelCase if word contains underscores
and the other way around otherwise"
(interactive)
(save-excursion
(destructuring-bind (beg . end) (bounds-of-thing-at-point 'word)
(goto-char beg)
(cond
((save-excursion (re-search-forward "_" end t))
(underscore-to-camelcase))
(t (camelcase-to-underscore))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment