A very old question, I know, but I just achieved this using "pure emacs". In short, the solution is as follows:
Run Mx query-replace-regexp . When prompted, enter
/\*\(\(.\|^J\)*?\)*\*/
as a regular expression to search for. ^J is a new line that you can enter by pressing ^Q (Ctrl + Q on most keyboards) and then pressing the enter key. Then enter
//\,(replace-regexp-in-string "[\n]\\([ ]*?\\) \\([^ ]\\)" "\n\\1// \\2" \1))
as a replacement expression.
Essentially, the idea is that you use two nested searches in a regular expression. The main one simply finds C-style comments (it’s very convenient to use *? Impatient repetition for this). Then, the elisp expression is used to perform the second replacement inside the comment text. In this case, I am looking for new lines followed by a space, and replacing the last three space characters with // , which is good for preserving the formatting of comments (only works as long as all comments are indented).
Changes in the secondary regular expression will make this approach work in other cases, for example
//\,(replace-regexp-in-string "[\n]" " " \1))
just put the entire contents of the original comment in a single C ++ style comment.
msoto
source share