[gnu.emacs] Commenting out C++ Code

metz@iam.unibe.ch (Igor Metz) (09/05/89)

Last week Dave Detlefs posted a new version of c++-mode. Below you'll find
a diff for it which adds two functions for commenting and uncommenting code.

*** /usr/local/src/emacs/c++-mode.el	Thu Aug 31 11:15:03 1989
--- c++-mode.el	Tue Sep  5 15:32:23 1989
***************
*** 4,9 ****
--- 4,13 ----
  ;; Done by fairly faithful modification of:
  ;; c-mode.el, Copyright (C) 1985 Richard M. Stallman.
  ;;
+ ;; Sep 5, 1989: Igor Metz <metz@iam.unibe.ch>
+ ;;   Added functions c++-comment-region and c++-uncomment-region and 
+ ;;   corresponding key-binding.
+ ;;
  ;; Aug 7, 1989; John Hagerman (hagerman@ece.cmu.edu):
  ;;   Changed calculate-c++-indent to handle member initializations
  ;;   more flexibly. Two new variables are used to control behavior:
***************
*** 64,69 ****
--- 68,75 ----
  (define-key c++-mode-map "\177" 'backward-delete-char-untabify)
  (define-key c++-mode-map "\t" 'c++-indent-command)
  (define-key c++-mode-map "\C-c\C-i" 'c++-insert-header)
+ (define-key c++-mode-map "\C-c\C-c" 'c++-comment-region)
+ (define-key c++-mode-map "\C-c\C-u" 'c++-uncomment-region)
  (define-key c++-mode-map "\C-c\C-\\" 'c++-macroize-region)
  (define-key c++-mode-map "\e\C-a" 'c++-beginning-of-defun)
  (define-key c++-mode-map "\e\C-e" 'c++-end-of-defun)
***************
*** 925,927 ****
--- 931,961 ----
  	(next-line 1)
  	(beginning-of-line 1)))
      (goto-char restore)))
+ 
+ (defun c++-comment-region ()
+   "Comment out all lines in a region between mark and current point by
+ inserting \"// \" (comment-start)in front of each line."
+   (interactive)
+   (let ((start (mark)))
+     (save-excursion
+       (while (>= (point) start)
+ 	(beginning-of-line)
+ 	(insert comment-start)
+ 	(previous-line 1)))))
+ 
+ 
+ (defun c++-uncomment-region ()
+   "Uncomment all lines in region between mark and current point by deleting
+ the leading \"// \" from each line, if any."
+   (interactive)
+   (let ((start (mark))
+ 	(n     (length comment-start))
+ 	(char  (string-to-char comment-start)))
+     (save-excursion
+       (while (>= (point) start)
+ 	(beginning-of-line)
+ 	(if (looking-at (concat " *" comment-start))
+ 	  (progn
+ 	    (zap-to-char 1 char)
+ 	    (delete-char n)))
+ 	(previous-line 1)))))
-- 

Igor Metz                    X400: metz@iam.unibe.ch
Institut fuer Informatik     ARPA: metz%iam.unibe.ch@relay.cs.net
und angewandte Mathematik    UUCP: ..!uunet!mcvax!iam.unibe.ch!metz