Wednesday, September 12, 2007

Commenting out a block of C++ in emacs

Here are couple more simple functions, used for commenting out a block of C++ code.

c++-comment-line
below simple inserts "//" at the start of a line

c++-comment region will take the current region and run the c++comment-line on each line.


(defun c++-comment-region()
"Comment a region out"
(interactive)
(save-excursion
(save-restriction
(narrow-to-region (point) (mark))
(goto-char (point-min))
(while (> (point-max) (point))
(c++-comment-line)
(forward-line)))))

(defun c++-comment-line()
"Comment a line of C++ out"
(beginning-of-line)
(insert "//"))

3 comments:

Alan Shutko said...

Any reason that M-x comment-dwim (bound by default to M-;) doesn't do what you want?

Justin said...

Nope and thanks for the heads up.

justin bartholomew said...

Interesting and important information. It is really beneficial for us. Thanks