- (if (or (/= (forward-line 1) 0)
- (>= (point) (cdr lit-limits))
- (and (looking-at "[ \t]*\\*/")
- (eq (cdr lit-limits) (match-end 0)))
- (and (looking-at prefix-regexp)
- (<= (1- (cdr lit-limits)) (match-end 0)))
- (and (< here (point))
- (or (not (match-beginning 0))
- (looking-at "[ \t]*\\\\?$"))))
- ;; The comment is either one line or the next line
- ;; contains just the comment ender. Also, if point is
- ;; on the comment opener line and the following line is
- ;; empty or doesn't match c-current-comment-prefix we
- ;; assume that this is in fact a not yet closed one line
- ;; comment, so we shouldn't look for the comment prefix
- ;; on the next line. In these cases we have no
- ;; information about a suitable comment prefix, so we
- ;; resort to c-block-comment-prefix.
- (setq comment-prefix (or c-block-comment-prefix "")
- res (let ((buffer-modified (buffer-modified-p))
- (buffer-undo-list t)
- tmp-pre tmp-post)
- ;; The comment doesn't give any information
- ;; about the indentation column. We'll have to
- ;; temporarily insert a new comment line and
- ;; indent it to find the correct column.
- (unwind-protect
- (progn
- (goto-char (car lit-limits))
- (if (looking-at comment-start-regexp)
- (goto-char (min (match-end 0)
- comment-text-end))
- (forward-char 2)
- (skip-chars-forward " \t"))
- (when (eq (char-syntax (char-before)) ?\ )
- ;; If there's ws on the current
- ;; line, we'll use it instead of
- ;; what's ending comment-prefix.
- (setq comment-prefix
- (concat (substring comment-prefix
- 0 (string-match
- "\\s *\\'"
- comment-prefix))
- (buffer-substring-no-properties
- (save-excursion
- (skip-chars-backward " \t")
- (point))
- (point)))))
- (setq tmp-pre (point-marker))
- ;; We insert an extra non-whitespace
- ;; character before the line break and
- ;; after comment-prefix in case it's
- ;; "" or ends with whitespace.
- (insert-and-inherit "x\n" comment-prefix "x")
- (setq tmp-post (point-marker))
- (indent-according-to-mode)
- (goto-char (1- tmp-post))
- (cons (buffer-substring-no-properties
- (c-point 'bol) (point))
- (current-column)))
- (when tmp-post
- (delete-region tmp-pre tmp-post)
- (set-marker tmp-pre nil)
- (set-marker tmp-post nil))
- (set-buffer-modified-p buffer-modified))))
- ;; Otherwise the line after the comment starter is good
- ;; enough to find the prefix in.
- (setq prefix-line (point)))))))
- (or res
+
+ (cond ((or (/= (forward-line 1) 0)
+ (>= (point) (cdr lit-limits))
+ (and (looking-at "[ \t]*\\*/")
+ (eq (cdr lit-limits) (match-end 0)))
+ (and (looking-at prefix-regexp)
+ (<= (1- (cdr lit-limits)) (match-end 0))))
+ ;; The comment is either one line or the next line contains
+ ;; just the comment ender. In this case we have no
+ ;; information about a suitable comment prefix, so we resort
+ ;; to c-block-comment-prefix.
+ (setq comment-prefix (or c-block-comment-prefix "")))
+
+ ((< here (point))
+ ;; The point was on the comment opener line, so we might want
+ ;; to treat this as a not yet closed comment.
+
+ (if (and (match-beginning 1)
+ (/= (match-beginning 1) (match-end 1)))
+ ;; Above `prefix-regexp' matched a nonempty prefix on the
+ ;; second line, so let's use it. Normally it should do
+ ;; to set `prefix-line' and let the code below pick up
+ ;; the whole prefix, but if there's no text after the
+ ;; match then it will probably fall back to no prefix at
+ ;; all if the comment isn't closed yet, so in that case
+ ;; it's better to force use of the prefix matched now.
+ (if (= (match-end 0) (c-point 'eol))
+ (setq comment-prefix (match-string 1))
+ (setq prefix-line (point)))
+
+ ;; There's no nonempty prefix on the line after the
+ ;; comment opener. If the line is empty, or if the
+ ;; text on it has less or equal indentation than the
+ ;; comment starter we assume it's an unclosed
+ ;; comment starter, i.e. that
+ ;; `c-block-comment-prefix' should be used.
+ ;; Otherwise we assume it's a closed comment where
+ ;; the prefix really is the empty string.
+ ;; E.g. this is an unclosed comment:
+ ;;
+ ;; /*
+ ;; foo
+ ;;
+ ;; But this is not:
+ ;;
+ ;; /*
+ ;; foo
+ ;; */
+ ;;
+ ;; (Looking for the presence of the comment closer
+ ;; rarely works since it's probably the closer of
+ ;; some comment further down when the comment
+ ;; really is unclosed.)
+ (if (<= (save-excursion (back-to-indentation)
+ (current-column))
+ (save-excursion (goto-char (car lit-limits))
+ (current-column)))
+ (setq comment-prefix (or c-block-comment-prefix ""))
+ (setq prefix-line (point)))))
+
+ (t
+ ;; Otherwise the line after the comment starter is good
+ ;; enough to find the prefix in.
+ (setq prefix-line (point))))
+
+ (when comment-prefix
+ ;; Haven't got the comment prefix on any real line that we
+ ;; can take it from, so we have to temporarily insert
+ ;; `comment-prefix' on a line and indent it to find the
+ ;; correct column and the correct mix of tabs and spaces.
+ (setq res
+ (let (tmp-pre tmp-post)
+ (unwind-protect
+ (progn
+
+ (goto-char (car lit-limits))
+ (if (looking-at comment-start-regexp)
+ (goto-char (min (match-end 0)
+ comment-text-end))
+ (forward-char 2)
+ (skip-chars-forward " \t"))
+
+ (when (eq (char-syntax (char-before)) ?\ )
+ ;; If there's ws on the current line, we'll use it
+ ;; instead of what's ending comment-prefix.
+ (setq comment-prefix
+ (concat (substring comment-prefix
+ 0 (string-match
+ "\\s *\\'"
+ comment-prefix))
+ (buffer-substring-no-properties
+ (save-excursion
+ (skip-chars-backward " \t")
+ (point))
+ (point)))))
+
+ (setq tmp-pre (point-marker))
+
+ ;; We insert an extra non-whitespace character
+ ;; before the line break and after comment-prefix in
+ ;; case it's "" or ends with whitespace.
+ (insert-and-inherit "x\n" comment-prefix "x")
+ (setq tmp-post (point-marker))
+
+ (indent-according-to-mode)
+
+ (goto-char (1- tmp-post))
+ (cons (buffer-substring-no-properties
+ (c-point 'bol) (point))
+ (current-column)))
+
+ (when tmp-post
+ (delete-region tmp-pre tmp-post)
+ (set-marker tmp-pre nil)
+ (set-marker tmp-post nil))))))))))
+
+ (or res ; Found a good prefix above.
+