Skip to content

Instantly share code, notes, and snippets.

@til
Created April 29, 2014 14:17
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 til/11401729 to your computer and use it in GitHub Desktop.
Save til/11401729 to your computer and use it in GitHub Desktop.
(defun downcase-word-with-camelcase ()
"This works similar to downcase-word, but additionally inserts
an underscore before upper case chars within camel cased words, so that:
FooBar => foo_bar"
(interactive)
(progn
(if (looking-at-p "\\>") (search-forward-regexp "\\<"))
(let ((start (point)))
(search-forward-regexp "\\>")
(let ((end (point)))
(let ((word (buffer-substring start end)))
(delete-region start end)
(insert
(downcase (let ((case-fold-search nil))
(replace-regexp-in-string "\\([a-z]\\)\\([A-Z]\\)" "\\1_\\2" word))))
)))))
(global-set-key (kbd "M-l") 'downcase-word-with-camelcase)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment