mirror of
git://git.sv.gnu.org/emacs.git
synced 2026-02-22 12:57:34 +00:00
(c-fill-paragraph): Handle C++ mode.
This commit is contained in:
parent
0af6a8312e
commit
0fef40c099
1 changed files with 153 additions and 110 deletions
|
|
@ -286,73 +286,11 @@ preserving the comment indentation or line-starting decorations."
|
|||
(skip-chars-forward " \t\n")
|
||||
(and (looking-at comment-start-skip)
|
||||
(setq comment-start-place (point))))))
|
||||
(if (or first-line
|
||||
;; t if we enter a comment between start of function and this line.
|
||||
(eq (calculate-c-indent) t)
|
||||
;; t if this line contains a comment starter.
|
||||
(setq first-line
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(prog1
|
||||
(re-search-forward comment-start-skip
|
||||
(save-excursion (end-of-line)
|
||||
(point))
|
||||
t)
|
||||
(setq comment-start-place (point))))))
|
||||
;; Inside a comment: fill one comment paragraph.
|
||||
(let ((fill-prefix
|
||||
;; The prefix for each line of this paragraph
|
||||
;; is the appropriate part of the start of this line,
|
||||
;; up to the column at which text should be indented.
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(if (looking-at "[ \t]*/\\*.*\\*/")
|
||||
(progn (re-search-forward comment-start-skip)
|
||||
(make-string (current-column) ?\ ))
|
||||
(if first-line (forward-line 1))
|
||||
|
||||
(let ((line-width (progn (end-of-line) (current-column))))
|
||||
(beginning-of-line)
|
||||
(prog1
|
||||
(buffer-substring
|
||||
(point)
|
||||
|
||||
;; How shall we decide where the end of the
|
||||
;; fill-prefix is?
|
||||
;; calculate-c-indent-within-comment bases its value
|
||||
;; on the indentation of previous lines; if they're
|
||||
;; indented specially, it could return a column
|
||||
;; that's well into the current line's text. So
|
||||
;; we'll take at most that many space, tab, or *
|
||||
;; characters, and use that as our fill prefix.
|
||||
(let ((max-prefix-end
|
||||
(progn
|
||||
(move-to-column
|
||||
(calculate-c-indent-within-comment t)
|
||||
t)
|
||||
(point))))
|
||||
(beginning-of-line)
|
||||
(skip-chars-forward " \t*" max-prefix-end)
|
||||
(point)))
|
||||
|
||||
;; If the comment is only one line followed by a blank
|
||||
;; line, calling move-to-column above may have added
|
||||
;; some spaces and tabs to the end of the line; the
|
||||
;; fill-paragraph function will then delete it and the
|
||||
;; newline following it, so we'll lose a blank line
|
||||
;; when we shouldn't. So delete anything
|
||||
;; move-to-column added to the end of the line. We
|
||||
;; record the line width instead of the position of the
|
||||
;; old line end because move-to-column might break a
|
||||
;; tab into spaces, and the new characters introduced
|
||||
;; there shouldn't be deleted.
|
||||
|
||||
;; If you can see a better way to do this, please make
|
||||
;; the change. This seems very messy to me.
|
||||
(delete-region (progn (move-to-column line-width)
|
||||
(point))
|
||||
(progn (end-of-line) (point))))))))
|
||||
|
||||
(if (and (eq major-mode 'c++-mode)
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(looking-at ".*//")))
|
||||
(let (fill-prefix
|
||||
(paragraph-start
|
||||
;; Lines containing just a comment start or just an end
|
||||
;; should not be filled into paragraphs they are next to.
|
||||
|
|
@ -362,49 +300,154 @@ preserving the comment indentation or line-starting decorations."
|
|||
(paragraph-separate
|
||||
(concat
|
||||
paragraph-separate
|
||||
"\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
|
||||
(chars-to-delete 0))
|
||||
(save-restriction
|
||||
;; Don't fill the comment together with the code following it.
|
||||
;; So temporarily exclude everything before the comment start,
|
||||
;; and everything after the line where the comment ends.
|
||||
;; If comment-start-place is non-nil, the comment starter is there.
|
||||
;; Otherwise, point is inside the comment.
|
||||
(narrow-to-region (save-excursion
|
||||
(if comment-start-place
|
||||
(goto-char comment-start-place)
|
||||
(search-backward "/*"))
|
||||
;; Protect text before the comment start
|
||||
;; by excluding it. Add spaces to bring back
|
||||
;; proper indentation of that point.
|
||||
(let ((column (current-column)))
|
||||
(prog1 (point)
|
||||
(setq chars-to-delete column)
|
||||
(insert-char ?\ column))))
|
||||
(save-excursion
|
||||
(if comment-start-place
|
||||
(goto-char (+ comment-start-place 2)))
|
||||
(search-forward "*/" nil 'move)
|
||||
(forward-line 1)
|
||||
(point)))
|
||||
|
||||
(fill-paragraph arg)
|
||||
(save-excursion
|
||||
;; Delete the chars we inserted to avoid clobbering
|
||||
;; the stuff before the comment start.
|
||||
(goto-char (point-min))
|
||||
(if (> chars-to-delete 0)
|
||||
(delete-region (point) (+ (point) chars-to-delete)))
|
||||
;; Find the comment ender (should be on last line of buffer,
|
||||
;; given the narrowing) and don't leave it on its own line.
|
||||
(goto-char (point-max))
|
||||
(forward-line -1)
|
||||
(search-forward "*/" nil 'move)
|
||||
(beginning-of-line)
|
||||
(if (looking-at "[ \t]*\\*/")
|
||||
(delete-indentation)))))
|
||||
;; Outside of comments: do ordinary filling.
|
||||
(fill-paragraph arg))))
|
||||
"\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$")))
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
;; Move up to first line of this comment.
|
||||
(while (and (not (bobp)) (looking-at "[ \t]*//"))
|
||||
(forward-line -1))
|
||||
(if (not (looking-at ".*//"))
|
||||
(forward-line 1))
|
||||
;; Find the comment start in this line.
|
||||
(re-search-forward "[ \t]*//[ \t]*")
|
||||
;; Set the fill-prefix to be what all lines except the first
|
||||
;; should start with.
|
||||
(let ((endcol (current-column)))
|
||||
(skip-chars-backward " \t")
|
||||
(setq fill-prefix
|
||||
(concat (make-string (- (current-column) 2) ?\ )
|
||||
"//"
|
||||
(make-string (- endcol (current-column)) ?\ ))))
|
||||
(save-restriction
|
||||
;; Narrow down to just the lines of this comment.
|
||||
(narrow-to-region (point)
|
||||
(save-excursion
|
||||
(forward-line 1)
|
||||
(while (looking-at "[ \t]*//"))
|
||||
(forward-line 1))
|
||||
(point)))
|
||||
(insert fill-prefix)
|
||||
(fill-paragraph arg)
|
||||
(delete-region (point-min)
|
||||
(+ (point-min) (length fill-prefix))))))
|
||||
(if (or first-line
|
||||
;; t if we enter a comment between start of function and this line.
|
||||
(eq (calculate-c-indent) t)
|
||||
;; t if this line contains a comment starter.
|
||||
(setq first-line
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(prog1
|
||||
(re-search-forward comment-start-skip
|
||||
(save-excursion (end-of-line)
|
||||
(point))
|
||||
t)
|
||||
(setq comment-start-place (point))))))
|
||||
;; Inside a comment: fill one comment paragraph.
|
||||
(let ((fill-prefix
|
||||
;; The prefix for each line of this paragraph
|
||||
;; is the appropriate part of the start of this line,
|
||||
;; up to the column at which text should be indented.
|
||||
(save-excursion
|
||||
(beginning-of-line)
|
||||
(if (looking-at "[ \t]*/\\*.*\\*/")
|
||||
(progn (re-search-forward comment-start-skip)
|
||||
(make-string (current-column) ?\ ))
|
||||
(if first-line (forward-line 1))
|
||||
|
||||
(let ((line-width (progn (end-of-line) (current-column))))
|
||||
(beginning-of-line)
|
||||
(prog1
|
||||
(buffer-substring
|
||||
(point)
|
||||
|
||||
;; How shall we decide where the end of the
|
||||
;; fill-prefix is?
|
||||
;; calculate-c-indent-within-comment bases its value
|
||||
;; on the indentation of previous lines; if they're
|
||||
;; indented specially, it could return a column
|
||||
;; that's well into the current line's text. So
|
||||
;; we'll take at most that many space, tab, or *
|
||||
;; characters, and use that as our fill prefix.
|
||||
(let ((max-prefix-end
|
||||
(progn
|
||||
(move-to-column
|
||||
(calculate-c-indent-within-comment t)
|
||||
t)
|
||||
(point))))
|
||||
(beginning-of-line)
|
||||
(skip-chars-forward " \t*" max-prefix-end)
|
||||
(point)))
|
||||
|
||||
;; If the comment is only one line followed by a blank
|
||||
;; line, calling move-to-column above may have added
|
||||
;; some spaces and tabs to the end of the line; the
|
||||
;; fill-paragraph function will then delete it and the
|
||||
;; newline following it, so we'll lose a blank line
|
||||
;; when we shouldn't. So delete anything
|
||||
;; move-to-column added to the end of the line. We
|
||||
;; record the line width instead of the position of the
|
||||
;; old line end because move-to-column might break a
|
||||
;; tab into spaces, and the new characters introduced
|
||||
;; there shouldn't be deleted.
|
||||
|
||||
;; If you can see a better way to do this, please make
|
||||
;; the change. This seems very messy to me.
|
||||
(delete-region (progn (move-to-column line-width)
|
||||
(point))
|
||||
(progn (end-of-line) (point))))))))
|
||||
|
||||
(paragraph-start
|
||||
;; Lines containing just a comment start or just an end
|
||||
;; should not be filled into paragraphs they are next to.
|
||||
(concat
|
||||
paragraph-start
|
||||
"\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
|
||||
(paragraph-separate
|
||||
(concat
|
||||
paragraph-separate
|
||||
"\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[ \t/*]*$"))
|
||||
(chars-to-delete 0))
|
||||
(save-restriction
|
||||
;; Don't fill the comment together with the code following it.
|
||||
;; So temporarily exclude everything before the comment start,
|
||||
;; and everything after the line where the comment ends.
|
||||
;; If comment-start-place is non-nil, the comment starter is there.
|
||||
;; Otherwise, point is inside the comment.
|
||||
(narrow-to-region (save-excursion
|
||||
(if comment-start-place
|
||||
(goto-char comment-start-place)
|
||||
(search-backward "/*"))
|
||||
;; Protect text before the comment start
|
||||
;; by excluding it. Add spaces to bring back
|
||||
;; proper indentation of that point.
|
||||
(let ((column (current-column)))
|
||||
(prog1 (point)
|
||||
(setq chars-to-delete column)
|
||||
(insert-char ?\ column))))
|
||||
(save-excursion
|
||||
(if comment-start-place
|
||||
(goto-char (+ comment-start-place 2)))
|
||||
(search-forward "*/" nil 'move)
|
||||
(forward-line 1)
|
||||
(point)))
|
||||
(fill-paragraph arg)
|
||||
(save-excursion
|
||||
;; Delete the chars we inserted to avoid clobbering
|
||||
;; the stuff before the comment start.
|
||||
(goto-char (point-min))
|
||||
(if (> chars-to-delete 0)
|
||||
(delete-region (point) (+ (point) chars-to-delete)))
|
||||
;; Find the comment ender (should be on last line of buffer,
|
||||
;; given the narrowing) and don't leave it on its own line.
|
||||
(goto-char (point-max))
|
||||
(forward-line -1)
|
||||
(search-forward "*/" nil 'move)
|
||||
(beginning-of-line)
|
||||
(if (looking-at "[ \t]*\\*/")
|
||||
(delete-indentation)))))
|
||||
;; Outside of comments: do ordinary filling.
|
||||
(fill-paragraph arg)))))
|
||||
|
||||
(defun electric-c-brace (arg)
|
||||
"Insert character and correct line's indentation."
|
||||
|
|
|
|||
Loading…
Reference in a new issue