hide-lines.el
changeset 22 a7906973ceb5
equal deleted inserted replaced
21:a48713acd5c1 22:a7906973ceb5
       
     1 ;;; hide-lines.el --- Commands for hiding lines based on a regexp
       
     2 ;; 
       
     3 ;;; Author: Mark Hulme-Jones <ture at plig cucumber dot net>
       
     4 ;; 
       
     5 ;;; History
       
     6 ;; 
       
     7 ;; 24/03/2004 - Incorporate fix for infinite loop bug from David
       
     8 ;; Hansen
       
     9 ;; 
       
    10 ;;; Commentary
       
    11 ;; 
       
    12 ;; The simplest way to make hide-lines work is to add the following
       
    13 ;; lines to your .emacs file:
       
    14 ;; 
       
    15 ;; (autoload 'hide-lines "hide-lines" "Hide lines based on a regexp" t)
       
    16 ;; (global-set-key "\C-ch" 'hide-lines)
       
    17 ;; 
       
    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.
       
    21 ;; 
       
    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.
       
    25 ;; 
       
    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.
       
    30 
       
    31 (defvar invisible-areas-list ()
       
    32  "List of invisible overlays used by hidelines")
       
    33 
       
    34 (add-to-invisibility-spec 'hl)
       
    35 
       
    36 (defun hide-lines (&optional arg)
       
    37   "Hide lines matching the specified regexp.
       
    38 With prefix arg: Hide lines that do not match the specified regexp"
       
    39   (interactive "p")
       
    40   (if (> arg 1)
       
    41       (call-interactively 'hide-non-matching-lines)
       
    42       (call-interactively 'hide-matching-lines)))
       
    43 
       
    44 (defun add-invisible-overlay (start end)
       
    45   "Add an overlay from `start' to `end' in the current buffer.  Push the
       
    46 overlay 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)))
       
    50 
       
    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)
       
    56   (save-excursion 
       
    57     (goto-char (point-min))
       
    58     (let ((start-position (point-min))
       
    59           (pos (re-search-forward search-text nil t)))
       
    60       (while pos
       
    61         (beginning-of-line)
       
    62         (add-invisible-overlay start-position (point))
       
    63         (forward-line 1)
       
    64         (setq start-position (point))
       
    65         (if (eq (point) (point-max))
       
    66             (setq pos nil)
       
    67           (setq pos (re-search-forward search-text nil t))))
       
    68       (add-invisible-overlay start-position (point-max)))))
       
    69 
       
    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)
       
    75   (save-excursion
       
    76     (goto-char (point-min))
       
    77     (let ((pos (re-search-forward search-text nil t))
       
    78           start-position)
       
    79       (while pos
       
    80         (beginning-of-line)
       
    81         (setq start-position (point))
       
    82         (end-of-line)
       
    83         (add-invisible-overlay start-position (+ 1 (point)))
       
    84         (forward-line 1)
       
    85         (if (eq (point) (point-max))
       
    86             (setq pos nil)
       
    87           (setq pos (re-search-forward search-text nil t)))))))
       
    88 
       
    89 (defun show-all-invisible ()
       
    90   "Show all areas hidden by the filter-buffer command"
       
    91   (interactive)
       
    92   (mapcar (lambda (overlay) (delete-overlay overlay)) 
       
    93           invisible-areas-list)
       
    94   (setq invisible-areas-list ()))
       
    95 
       
    96 (provide 'hide-lines)