(defun make-lines-invisible (regexp &optional arg) "Make all lines matching a regexp invisible and intangible. With a prefix arg, make them visible again. It is not necessary that REGEXP matches the whole line; if a hit is found, the affected line gets automatically selected. This function affects the whole buffer. Note that this function modifies the `invisible' and `intangible' text properties; it may thus interfere with modes which use them. Due to implementation restrictions in current Emacs versions it is not possible to use overlays -- which would avoid text property modifications -- without becoming unbearably slow for large buffers with many matches." (interactive "MRegexp: \nP") (save-excursion (cond (arg (let ((next-pos (point-min))) (while (setq next-pos (text-property-any next-pos (point-max) 'make-lines-invisible t)) (goto-char next-pos) (setq next-pos (or (next-single-property-change (point) 'make-lines-invisible) (point-max))) (remove-list-of-text-properties (point) next-pos '(make-lines-invisible invisible intangible))))) (t (goto-char (point-min)) (while (re-search-forward regexp nil t) (add-text-properties (line-beginning-position) (1+ (line-end-position)) ; handle \n '(make-lines-invisible t invisible t intangible t)) (goto-char (line-end-position))))))) (global-set-key "\C-cz" 'make-lines-invisible)