1;;; hide-lines.el --- Commands for hiding lines based on a regexp
3;;; Author: Mark Hulme-Jones <ture at plig cucumber dot net>
7;; 24/03/2004 - Incorporate fix for infinite loop bug from David
12;; The simplest way to make hide-lines work is to add the following
13;; lines to your .emacs file:
15;; (autoload 'hide-lines "hide-lines" "Hide lines based on a regexp" t)
16;; (global-set-key "\C-ch" 'hide-lines)
18;; Now, when you type C-c h, you will be prompted for a regexp
19;; (regular expression). All lines matching this regexp will be
20;; hidden in the buffer.
22;; Alternatively, you can type C-u C-c h (ie. provide a prefix
23;; argument to the hide-lines command) to hide all lines that *do not*
24;; match the specified regexp.
26;; If you want to make all of the hidden areas re-appear again, type:
27;; M-x show-all-invisible
28;; Or you can bind show-all-invisible to a key and use that to show
29;; all the hidden areas again.
31(defvar invisible-areas-list ()
32 "List of invisible overlays used by hidelines")
34(add-to-invisibility-spec 'hl)
36(defun hide-lines (&optional arg)
37 "Hide lines matching the specified regexp.
38With prefix arg: Hide lines that do not match the specified regexp"
41 (call-interactively 'hide-non-matching-lines)
42 (call-interactively 'hide-matching-lines)))
44(defun add-invisible-overlay (start end)
45 "Add an overlay from `start' to `end' in the current buffer. Push the
46overlay onto the invisible-areas-list list"
47 (let ((overlay (make-overlay start end)))
48 (setq invisible-areas-list (cons overlay invisible-areas-list))
49 (overlay-put overlay 'invisible 'hl)))
51(defun hide-non-matching-lines (search-text)
52 "Hide lines that don't match the specified regexp."
53 (interactive "MHide lines not matched by regexp: ")
54 (make-variable-buffer-local 'line-move-ignore-invisible)
55 (setq line-move-ignore-invisible t)
57 (goto-char (point-min))
58 (let ((start-position (point-min))
59 (pos (re-search-forward search-text nil t)))
62 (add-invisible-overlay start-position (point))
64 (setq start-position (point))
65 (if (eq (point) (point-max))
67 (setq pos (re-search-forward search-text nil t))))
68 (add-invisible-overlay start-position (point-max)))))
70(defun hide-matching-lines (search-text)
71 "Hide lines matching the specified regexp."
72 (interactive "MHide lines matching regexp: ")
73 (make-variable-buffer-local 'line-move-ignore-invisible)
74 (setq line-move-ignore-invisible t)
76 (goto-char (point-min))
77 (let ((pos (re-search-forward search-text nil t))
81 (setq start-position (point))
83 (add-invisible-overlay start-position (+ 1 (point)))
85 (if (eq (point) (point-max))
87 (setq pos (re-search-forward search-text nil t)))))))
89(defun show-all-invisible ()
90 "Show all areas hidden by the filter-buffer command"
92 (mapcar (lambda (overlay) (delete-overlay overlay))
94 (setq invisible-areas-list ()))