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 ☺).
The European Copyright directive threatens online communication in Europe.
But thanks to massive shared action earlier this year, the European parliament can still prevent the problems. For each of the articles there are proposals which fix them. The parliamentarians (MEPs) just have to vote for them. And since they are under massive pressure from large media companies, that went as far as defaming those who took action as fake people, the MEPs need to hear your voice to know that your are real.
If you care about the future of the Internet in the EU, please Call your MEPs.