Add a function to convert CamelCase to snake_case

This commit is contained in:
Gergely Polonkai 2018-03-19 12:12:29 +01:00
parent c4aff57910
commit dbe501a258
1 changed files with 21 additions and 0 deletions

View File

@ -42,6 +42,27 @@ to the beginning of the file."
(insert "\"\"\"")
(open-line-above)))
(defun camel-to-snake-case (arg)
"Convert a camel case (camelCase or CamelCase) word to snake case (snake_case).
If the prefix argument ARG is non-nil, convert the text to uppercase."
(interactive "p")
(progn
(let ((start (region-beginning))
(end (region-end))
(case-fold-search nil)
(had-initial-underscore nil))
(goto-char start)
(when (looking-at "_") (setq had-initial-underscore t))
(while (re-search-forward "\\([A-Z]\\)" end t)
(replace-match "_\\1")
(setq end (1+ end)))
(if arg
(upcase-region start end)
(downcase-region start end))
(goto-char start)
(unless had-initial-underscore (delete-char 1)))))
(provide 'gp-prog)
;;; gp-prog.el ends here