Function: c-lineup-C-comments

c-lineup-C-comments is a byte-compiled function defined in cc-align.el.gz.

Signature

(c-lineup-C-comments LANGELEM)

Documentation

Line up C block comment continuation lines.

Various heuristics are used to handle many of the common comment styles. Some examples:

/* /** /* /* text /* /**
 * text * text text text ** text ** text
 */ */ */ */ */ */

/*********************************************************************
 * text
 ********************************************************************/

/*********************************************************************
    Free form text comments:
 In comments with a long delimiter line at the start, the indentation
 is kept unchanged for lines that start with an empty comment line
 prefix. The delimiter line is whatever matches the
 comment-start-skip regexp.
*********************************************************************/

The variable c-comment-prefix-regexp is used to recognize the comment line prefix, e.g. the * that usually starts every line inside a comment.

Works with: The c syntactic symbol.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-align.el.gz
(defun c-lineup-C-comments (langelem)
  "Line up C block comment continuation lines.
Various heuristics are used to handle many of the common comment
styles.  Some examples:

/*          /**         /*         /* text      /*          /**
 * text      * text       text        text      ** text      ** text
 */          */         */         */           */           */

/*********************************************************************
 * text
 ********************************************************************/

/*********************************************************************
    Free form text comments:
 In comments with a long delimiter line at the start, the indentation
 is kept unchanged for lines that start with an empty comment line
 prefix.  The delimiter line is whatever matches the
 `comment-start-skip' regexp.
*********************************************************************/

The variable `c-comment-prefix-regexp' is used to recognize the
comment line prefix, e.g. the `*' that usually starts every line
inside a comment.

Works with: The `c' syntactic symbol."
  (save-excursion
    (let* ((here (point))
	   (prefixlen (progn (back-to-indentation)
			     (if (looking-at c-current-comment-prefix)
				 (- (match-end 0) (point))
			       0)))
	   (starterlen
	    ;; Get the length of the comment starter, not including
	    ;; the first '/'. We check if the comment prefix matched
	    ;; on the current line matches the starter or if it
	    ;; matches comment-start-skip, and choose whichever is
	    ;; longest.
	    (max (save-excursion
		   (goto-char (1+ (c-langelem-pos langelem)))
		   (if (and (match-string 0)
			    (looking-at (regexp-quote (match-string 0))))
		       (- (match-end 0) (match-beginning 0))
		     0))
		 (save-excursion
		   (goto-char (c-langelem-pos langelem))
		   (looking-at comment-start-skip)
		   (- (or (match-end 1)
			  (save-excursion
			    (goto-char (match-end 0))
			    (skip-chars-backward " \t")
			    (point)))
		      (point)
		      1)))))
      (if (and (> starterlen 10) (zerop prefixlen))
	  ;; The comment has a long starter and the line doesn't have
	  ;; a nonempty comment prefix.  Treat it as free form text
	  ;; and don't change the indentation.
	  (vector (current-column))
	;; Go back to the previous non-blank line, if any.
	(while
	    (progn
	      (forward-line -1)
	      (back-to-indentation)
	      (and (> (point) (c-langelem-pos langelem))
		   (looking-at "[ \t]*$"))))
	;; Is the starting line the first continuation line with content?
	(if (>= (c-langelem-pos langelem) (point))
	    (if (zerop prefixlen)
		;; No nonempty comment prefix. Align after comment
		;; starter.
		(progn
		  (looking-at comment-start-skip)
		  (goto-char (match-end 0))
		  ;; The following should not be necessary, since
		  ;; comment-start-skip should match everything (i.e.
		  ;; typically whitespace) that leads up to the text.
		  ;;(if (looking-at "\\([ \t]+\\).+$")
		  ;;    ;; Align with the text that hangs after the
		  ;;    ;; comment starter.
		  ;;    (goto-char (match-end 1)))
		  (vector (current-column)))
	      ;; How long is the comment starter?  if greater than the
	      ;; length of the comment prefix, align left.  if less
	      ;; than or equal, align right.  this should also pick up
	      ;; Javadoc style comments.
	      (if (> starterlen prefixlen)
		  (progn
		    (goto-char (c-langelem-pos langelem))
		    (vector (1+ (current-column))))
		(goto-char (+ (c-langelem-pos langelem) starterlen 1))
		(vector (- (current-column) prefixlen))))
	  ;; We didn't start on the first non-blank continuation line.  If the
	  ;; previous line has a nonempty comment prefix, align with it.
	  ;; Otherwise, align with the previous nonempty line, but align the
	  ;; comment ender with the starter.
	  (when (or (not (looking-at c-current-comment-prefix))
		    (eq (match-beginning 0) (match-end 0)))
	    (goto-char here)
	    (back-to-indentation)
	    (if (looking-at (concat "\\(" c-current-comment-prefix "\\)\\*/"))
		(goto-char (c-langelem-pos langelem))
	      (while (and (zerop (forward-line -1))
			  (looking-at "^[ \t]*$")))
	      (back-to-indentation)
	      (if (< (point) (c-langelem-pos langelem))
		  ;; Align with the comment starter rather than
		  ;; with the code before it.
		  (goto-char (c-langelem-pos langelem)))))
	  (vector (current-column)))))))