Function: c-context-line-break

c-context-line-break is an interactive and byte-compiled function defined in cc-cmds.el.gz.

Signature

(c-context-line-break)

Documentation

Do a line break suitable to the context.

When point is outside a comment or macro, insert a newline and indent according to the syntactic context, unless c-syntactic-indentation is nil, in which case the new line is indented as the previous non-empty line instead.

When point is inside the content of a preprocessor directive, a line continuation backslash is inserted before the line break and aligned appropriately. The end of the cpp directive doesn't count as inside it.

When point is inside a comment, continue it with the appropriate comment prefix (see the c-comment-prefix-regexp and c-block-comment-prefix variables for details). The end of a C++-style line comment doesn't count as inside it.

When point is inside a string, only insert a backslash when it is also inside a preprocessor directive.

View in manual

Probably introduced at or before Emacs version 21.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/cc-cmds.el.gz
(defun c-context-line-break ()
  "Do a line break suitable to the context.

When point is outside a comment or macro, insert a newline and indent
according to the syntactic context, unless `c-syntactic-indentation'
is nil, in which case the new line is indented as the previous
non-empty line instead.

When point is inside the content of a preprocessor directive, a line
continuation backslash is inserted before the line break and aligned
appropriately.  The end of the cpp directive doesn't count as inside
it.

When point is inside a comment, continue it with the appropriate
comment prefix (see the `c-comment-prefix-regexp' and
`c-block-comment-prefix' variables for details).  The end of a
C++-style line comment doesn't count as inside it.

When point is inside a string, only insert a backslash when it is also
inside a preprocessor directive."

  (interactive "*")
  (c-with-string-fences
   (let* (c-lit-limits c-lit-type
		       (c-macro-start c-macro-start)
		       case-fold-search)

     (c-save-buffer-state ()
       (setq c-lit-limits (c-literal-limits nil nil t)
	     c-lit-type (c-literal-type c-lit-limits))
       (when (eq c-lit-type 'c++)
	 (setq c-lit-limits (c-collect-line-comments c-lit-limits)))
       (c-query-and-set-macro-start))

     (cond
      ((or (eq c-lit-type 'c)
	   (and (eq c-lit-type 'c++) ; C++ comment, but not at the very end of it.
		(< (save-excursion
		     (skip-chars-forward " \t")
		     (point))
		   (1- (cdr c-lit-limits))))
	   (and (numberp c-macro-start)	; Macro, but not at the very end of
					; it, not in a string, and not in the
					; cpp keyword.
		(not (eq c-lit-type 'string))
		(or (not (looking-at "\\s *$"))
		    (eq (char-before) ?\\))
		(<= (save-excursion
		      (goto-char c-macro-start)
		      (if (looking-at c-opt-cpp-start)
			  (goto-char (match-end 0)))
		      (point))
		    (point))))
       (let ((comment-multi-line t)
	     (fill-prefix nil))
	 (c-indent-new-comment-line nil t)))

      ((eq c-lit-type 'string)
       (if (and (numberp c-macro-start)
		(not (eq (char-before) ?\\)))
	   (insert ?\\))
       (newline))

      (t (delete-horizontal-space)
	 (newline)
	 ;; c-indent-line may look at the current indentation, so let's
	 ;; start out with the same indentation as the previous line.
	 (let ((col (save-excursion
		      (backward-char)
		      (forward-line 0)
		      (while (and (looking-at "[ \t]*\\\\?$")
				  (= (forward-line -1) 0)))
		      (current-indentation))))
	   (indent-to col))
	 (indent-according-to-mode))))))