Function: kill-whole-line

kill-whole-line is an interactive and byte-compiled function defined in simple.el.gz.

Signature

(kill-whole-line &optional ARG)

Documentation

Kill current line.

With prefix ARG, kill that many lines starting from the current line. If ARG is negative, kill backward. Also kill the preceding newline.
(This is meant to make C-x z (repeat) work well with negative arguments.)
If ARG is zero, kill current line but exclude the trailing newline.

View in manual

Probably introduced at or before Emacs version 22.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun kill-whole-line (&optional arg)
  "Kill current line.
With prefix ARG, kill that many lines starting from the current line.
If ARG is negative, kill backward.  Also kill the preceding newline.
\(This is meant to make \\[repeat] work well with negative arguments.)
If ARG is zero, kill current line but exclude the trailing newline."
  (interactive "p")
  (or arg (setq arg 1))
  (if (and (> arg 0) (eobp) (save-excursion (forward-visible-line 0) (eobp)))
      (signal 'end-of-buffer nil))
  (if (and (< arg 0) (bobp) (save-excursion (end-of-visible-line) (bobp)))
      (signal 'beginning-of-buffer nil))
  (unless (eq last-command 'kill-region)
    (kill-new "")
    (setq last-command 'kill-region))
  ;; - We need to kill in two steps, because the previous command
  ;;   could have been a kill command, in which case the text before
  ;;   point needs to be prepended to the current kill ring entry and
  ;;   the text after point appended.
  ;; - We need to be careful to avoid copying text twice to the kill
  ;;   ring in read-only buffers.
  ;; - We need to determine the boundaries of visible lines before we
  ;;   do the first kill.  Otherwise `after-change-functions' may
  ;;   change visibility (bug#65734).
  (let (;; The beginning of both regions to kill
        (regions-begin (point-marker))
        ;; The end of the first region to kill.  Moreover, after
        ;; evaluation of the value form, (point) will be the end of
        ;; the second region to kill.
        (region1-end (cond ((zerop arg)
                            (prog1 (save-excursion
                                     (forward-visible-line 0)
                                     (point-marker))
                              (end-of-visible-line)))
                           ((< arg 0)
                            (prog1 (save-excursion
                                     (end-of-visible-line)
                                     (point-marker))
                              (forward-visible-line (1+ arg))
                              (unless (bobp) (backward-char))))
                           (t
                            (prog1 (save-excursion
                                     (forward-visible-line 0)
                                     (point-marker))
                              (forward-visible-line arg))))))
    ;; - Pass the marker positions and not the markers themselves.
    ;;   kill-region determines whether to prepend or append to a
    ;;   previous kill by checking the direction of the region.  But
    ;;   it deletes the content and hence moves the markers before
    ;;   that.  That effectively makes every region delimited by
    ;;   markers an (empty) forward region.
    ;; - Make the first kill-region emit a non-local exit only if the
    ;;   second kill-region below would not operate on a non-empty
    ;;   region.
    (let ((kill-read-only-ok (or kill-read-only-ok
                                 (/= regions-begin (point)))))
      (kill-region (marker-position regions-begin)
                   (marker-position region1-end)))
    (kill-region (marker-position regions-begin)
                 (point))
    (set-marker regions-begin nil)
    (set-marker region1-end nil)))