Today I created a simple Elisp function that will convert the text in a specified region from camel case (e.g. myFunction) to underscore separated format (e.g. my_Function). Glasses mode will do a similar thing in a non-destructive way using overlays. However, this function actually modifies the text.
I used this function to convert between a JDO JOQL filter string and a SQL where clause. The JDO reverse mapping tool we used took SQL table names like "mth_dim" and converted them to Java class names like "mthDim". I had to convert back.
(defun convert-camel-to-underscore (beg end)
"Converts the camel cased text in the specified region into
underscore separated text."
(interactive "r")
(save-excursion
(goto-char end)
(let ((case-fold-search nil)
(end-marker (point-marker)))
(goto-char beg)
(while (re-search-forward "\([a-z]\)\([A-Z]\)"
end-marker t)
(replace-match (concat (match-string 1) "_" (match-string 2)))))))
6:37:19 PM
|