Function: flush-lines

flush-lines is an interactive and byte-compiled function defined in replace.el.gz.

Signature

(flush-lines REGEXP &optional RSTART REND INTERACTIVE)

Documentation

Delete lines containing matches for REGEXP.

When called from Lisp (and usually when called interactively as well, see below), applies to the part of the buffer after point. The line point is in is deleted if and only if it contains a match for regexp starting after point.

If REGEXP contains upper case characters (excluding those preceded by \) and search-upper-case is non-nil, the matching is case-sensitive.

Second and third arg RSTART and REND specify the region to operate on. Lines partially contained in this region are deleted if and only if they contain a match entirely contained in it.

Interactively, in Transient Mark mode when the mark is active, operate on the contents of the region. Otherwise, operate from point to the end of (the accessible portion of) the buffer. When calling this function from Lisp, you can pretend that it was called interactively by passing a non-nil INTERACTIVE argument.

If a match is split across lines, all the lines it lies in are deleted. They are deleted _before_ looking for the next match. Hence, a match starting on the same line at which another match ended is ignored.

Return the number of deleted matching lines. When called interactively, also print the number.

If you want to not just delete the lines, but also add them to the kill ring, use the M-x kill-matching-lines (kill-matching-lines) command instead.

View in manual

Probably introduced at or before Emacs version 20.4.

Key Bindings

Aliases

delete-matching-lines

Source Code

;; Defined in /usr/src/emacs/lisp/replace.el.gz
(defun flush-lines (regexp &optional rstart rend interactive)
  "Delete lines containing matches for REGEXP.
When called from Lisp (and usually when called interactively as
well, see below), applies to the part of the buffer after point.
The line point is in is deleted if and only if it contains a
match for regexp starting after point.

If REGEXP contains upper case characters (excluding those preceded by `\\')
and `search-upper-case' is non-nil, the matching is case-sensitive.

Second and third arg RSTART and REND specify the region to operate on.
Lines partially contained in this region are deleted if and only if
they contain a match entirely contained in it.

Interactively, in Transient Mark mode when the mark is active, operate
on the contents of the region.  Otherwise, operate from point to the
end of (the accessible portion of) the buffer.  When calling this function
from Lisp, you can pretend that it was called interactively by passing
a non-nil INTERACTIVE argument.

If a match is split across lines, all the lines it lies in are deleted.
They are deleted _before_ looking for the next match.  Hence, a match
starting on the same line at which another match ended is ignored.

Return the number of deleted matching lines.  When called interactively,
also print the number.

If you want to not just delete the lines, but also add them to
the kill ring, use the \\[kill-matching-lines] command instead."
  (interactive
   (progn
     (barf-if-buffer-read-only)
     (keep-lines-read-args "Flush lines containing match for regexp")))
  (if rstart
      (progn
	(goto-char (min rstart rend))
	(setq rend (copy-marker (max rstart rend))))
    (if (and interactive (use-region-p))
	(setq rstart (region-beginning)
	      rend (copy-marker (region-end)))
      (setq rstart (point)
	    rend (point-max-marker)))
    (goto-char rstart))
  (let ((count 0)
        (case-fold-search
	 (if (and case-fold-search search-upper-case)
	     (isearch-no-upper-case-p regexp t)
	   case-fold-search)))
    (save-excursion
      (while (and (< (point) rend)
		  (re-search-forward regexp rend t))
	(delete-region (save-excursion (goto-char (match-beginning 0))
				       (forward-line 0)
				       (point))
		       (progn (forward-line 1) (point)))
        (setq count (1+ count))))
    (set-marker rend nil)
    (when interactive (message (ngettext "Deleted %d matching line"
					 "Deleted %d matching lines"
					 count)
			       count))
    count))