I currently cope with refactoring in an upstream project to which I maintain some changes which upstream does not merge. One nasty part is that the project converted from CamelCase for function names to words_with_underscores. And that created lots of merge errors.
Today I finally decided to speed up my work.
The first thing I needed was a function to convert a string in CamelCase to words_with_underscores. Since I’m lazy, I used google, and that turned up the CamelCase page of Emacswiki - and with it the following string functions:
(defun split-name (s) (split-string (let ((case-fold-search nil)) (downcase (replace-regexp-in-string "\\([a-z]\\)\\([A-Z]\\)" "\\1 \\2" s))) "[^A-Za-z0-9]+")) (defun underscore-string (s) (mapconcat 'downcase (split-name s) "_"))
Quite handy - and elegantly executed. Now I just need to make this available for interactive use. For this, Emacs Lisp offers many useful ways to turn Editor information into program information, called interactive codes - in my case the region-code: "r". This gives the function the beginning and the end of the currently selected region as arguments.
With this, I created an interactive function which de-camelcases and underscores the selected region:
(defun underscore-region (begin end) (interactive "r") (let* ((word (buffer-substring begin end)) (underscored (underscore-string word))) (save-excursion (widen) ; break out of the subregion so we can fix every usage of the function (replace-string word underscored nil (point-min) (point-max)))))
And now we’re almost there. Just create a macro which searches for a function, selects its name, de-camelcaeses and underscores it and then replaces every usage of the CamelCase name by the underscored name. This isn’t perfect refactoring (can lead to errors), but it’s fast and I see every change it does.
C-x C-( C-s def M-x mark-word M-x underscore-region C-x C-)
That’s it, now just call the macro repeatedly.
Now check the diff to fix where this 13 lines hack got something wrong ( like changing
_init_ - I won’t debug this, you’ve been warned ☺).
⚙ Babcom is trying to load the comments ⚙
This textbox will disappear when the comments have been loaded.
Note: To make a comment which isn’t a reply visible to others here, include a link to this site somewhere in the text of your comment. It will then show up here. To ensure that I get notified of your comment, also include my Sone-ID.
Link to this site and my Sone ID:
This spam-resistant comment-field is made with babcom.