[gnu.emacs.bug] fix for region commenting in c++-mode.el

metz@iam.unibe.ch (Igor Metz) (01/09/90)

several people (including me ..) found a bug in the region commenting code
of c++-mode.el.
The problem was, that commenting/uncommenting a region which begins at point 1
(or ends at (point-last)) caused a infinite loop.

Replace the two defuns in c++-mode.el with the code below.

--------- snip, snip, snip -------------
(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* ((m      (if (eq (mark) nil) (error "Mark is not set!") (mark)))
	 (start  (if (< (point) m) (point) m))
	 (end    (if (> (point) m) (point) m))
	 (mymark (copy-marker end)))
    (save-excursion
	(goto-char start)
	(while (< (point) (marker-position mymark))
	    (beginning-of-line)
	    (insert comment-start)
	    (beginning-of-line)
	    (next-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* ((m      (if (eq (mark) nil) (error "Mark is not set!") (mark)))
	 (start  (if (< (point) m) (point) m))
	 (end    (if (> (point) m) (point) m))
	 (mymark (copy-marker end))
	 (len    (length comment-start))
	 (char   (string-to-char comment-start))
	 )
    (save-excursion
	(goto-char start)
	(while (< (point) (marker-position mymark))
	    (beginning-of-line)
	    (if (looking-at (concat " *" comment-start))
		(progn
		  (zap-to-char 1 char)
		  (delete-char len)))
	    (beginning-of-line)
	    (next-line 1)
	    ))))
--------- snip, snip, snip -------------

Igor Metz                    X400: metz@iam.unibe.ch
Institut fuer Informatik     ARPA: metz%iam.unibe.ch@relay.cs.net
und angewandte Mathematik    UUCP: ..!uunet!mcsun!iam.unibe.ch!metz
Universitaet Bern            Phone: (0041) 31 65 49 90
Switzerland		     Fax:   (0041) 31 65 39 65