Function: newline

newline is an interactive and byte-compiled function defined in simple.el.gz.

Signature

(newline &optional ARG INTERACTIVE)

Documentation

Insert a newline, and move to left margin of the new line.

With prefix argument ARG, insert that many newlines.

If electric-indent-mode(var)/electric-indent-mode(fun) is enabled, this indents the final new line that it adds, and reindents the preceding line. To just insert a newline, use M-x electric-indent-just-newline (electric-indent-just-newline).

If auto-fill-mode is enabled, this may cause automatic line breaking of the preceding line. A non-nil ARG inhibits this.

If use-hard-newlines(var)/use-hard-newlines(fun) is enabled, the newline is marked with the text-property hard.

A non-nil INTERACTIVE argument means to run the post-self-insert-hook.

View in manual

Probably introduced at or before Emacs version 19.29.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun newline (&optional arg interactive)
   "Insert a newline, and move to left margin of the new line.
With prefix argument ARG, insert that many newlines.

If `electric-indent-mode' is enabled, this indents the final new line
that it adds, and reindents the preceding line.  To just insert
a newline, use \\[electric-indent-just-newline].

If `auto-fill-mode' is enabled, this may cause automatic line
breaking of the preceding line.  A non-nil ARG inhibits this.

If `use-hard-newlines' is enabled, the newline is marked with the
text-property `hard'.

A non-nil INTERACTIVE argument means to run the `post-self-insert-hook'."
  (interactive "*P\np")
  (barf-if-buffer-read-only)
  (when (and arg
             (< (prefix-numeric-value arg) 0))
    (error "Repetition argument has to be non-negative"))
  ;; Call self-insert so that auto-fill, abbrev expansion etc. happen.
  ;; Set last-command-event to tell self-insert what to insert.
  (let* ((was-page-start (and (bolp) (looking-at page-delimiter)))
         (beforepos (point))
         (last-command-event ?\n)
         ;; Don't auto-fill if we have a prefix argument.
         (inhibit-auto-fill (or inhibit-auto-fill arg))
         (arg (prefix-numeric-value arg))
         (procsym (make-symbol "newline-postproc")) ;(bug#46326)
         (postproc
          ;; Do the rest in post-self-insert-hook, because we want to do it
          ;; *before* other functions on that hook.
          (lambda ()
            (remove-hook 'post-self-insert-hook procsym t)
            ;; Mark the newline(s) `hard'.
            (if use-hard-newlines
                (set-hard-newline-properties
                 (- (point) arg) (point)))
            ;; If the newline leaves the previous line blank, and we
            ;; have a left margin, delete that from the blank line.
            (save-excursion
              (goto-char beforepos)
              (beginning-of-line)
              (and (looking-at "[ \t]+$")
                   (> (current-left-margin) 0)
                   (delete-region (point)
                                  (line-end-position))))
            ;; Indent the line after the newline, except in one case:
            ;; when we added the newline at the beginning of a line that
            ;; starts a page.
            (or was-page-start
                (move-to-left-margin nil t)))))
    (fset procsym postproc)
    (if (not interactive)
	;; FIXME: For non-interactive uses, many calls actually
	;; just want (insert "\n"), so maybe we should do just
	;; that, so as to avoid the risk of filling or running
	;; abbrevs unexpectedly.
	(let ((post-self-insert-hook (list postproc)))
	  (self-insert-command arg))
      (unwind-protect
	  (progn
	    (add-hook 'post-self-insert-hook procsym nil t)
	    (self-insert-command arg))
	;; We first used let-binding to protect the hook, but that
	;; was naive since add-hook affects the symbol-default
	;; value of the variable, whereas the let-binding might
	;; protect only the buffer-local value.
	(remove-hook 'post-self-insert-hook procsym t))))
  nil)