help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hiding lines based on a regexp


From: David Vanderschel
Subject: Re: Hiding lines based on a regexp
Date: Mon, 30 May 2005 04:57:03 GMT

"David Vanderschel" <address@hidden> wrote in message
news:address@hidden
> Could someone please point me to a mechanism that
> would allow me to temporarily hide from display
> certain lines in a file based on a matching regular
> expression?  ...

Well, no one did.  Although it is difficult for me to
believe that no one has done anything similar, I went
ahead and rolled my own mechanism.  It is not
particularly robust, but it is adequate for my
purposes:

______________________________________________________________________


(defun dv-mark-lines-invisible ( regexp )
"Set the `invisible' property for characters in matching lines to true.
Argument is a regular expression.  Each invocation marks an additional set
of
lines.  See related function `dv-unmark-lines-invisible'.  See
`dv-toggle-invisibility' for control of actual display.  See
`dv-all-visible' if
you forget any regexps you used to mark lines or if you wish, for any
reason,
to unmark all.  These functions will not play well with other functions
which
manipulate the `invisible' text property."
  (interactive "MMark for possible invisibility lines matching regexp: ")
  (let ( ( did  0 )
         ( nodo 0 )
         ( start-point (point-marker) ) )
    (goto-char (point-min))
    (while (search-forward-regexp regexp (point-max) t)
      (beginning-of-line)
      (let ( ( bol              (point) )
             ( invisiblity-prop (get-text-property (point) 'invisible) ) )
        (forward-line 1)
        (if invisiblity-prop
          (setq nodo (1+ nodo))
          (setq did  (1+ did ))
          (put-text-property bol (point) 'invisible t))))
    (redraw-display)
    (if (and (= did 0) (= nodo 0))
      (message "No matches.")
      (message
        (concat (format "Marked %d lines." did)
          (if (= nodo 0) "" (format "  %d matches were already marked."
nodo))
          (if buffer-invisibility-spec "" "  But all lines visible now."))))
    (goto-char start-point)))
(global-set-key [f6 ?8] 'dv-mark-lines-invisible)

(defun dv-unmark-lines-invisible ( regexp )
"Set the `invisible' property for characters in matching lines to nil.
See related and similar function `dv-mark-lines-invisible'."
  (interactive "MUnmark for possible invisibility lines matching regexp: ")
  (let ( ( did 0 )
         ( start-point (point-marker) ) )
    (goto-char (point-min))
    (while (search-forward-regexp regexp (point-max) t)
      (beginning-of-line)
      (let ( ( bol              (point) )
             ( invisiblity-prop (get-text-property (point) 'invisible) ) )
        (forward-line 1)
        (when invisiblity-prop
          (put-text-property bol (point) 'invisible nil)
          (setq did (1+ did)))))
    (redraw-display)
    (if (= did 0) (message "No matches."))
    (if (> did 0)
      (message
        (concat (format "Unmarked %d lines.  " did)
          (if buffer-invisibility-spec "" "But they were already
displayed."))))
    (goto-char start-point)))
(global-set-key [f6 ?9] 'dv-unmark-lines-invisible)

(defun dv-toggle-visibility ()
"Switch display of characters marked by `dv-mark-lines-invisible' by
toggling
`buffer-invisibility-spec'."
  (interactive)
  (setq buffer-invisibility-spec (not buffer-invisibility-spec))
  (redraw-display)
  (message (if buffer-invisibility-spec
             "Marked lines not visible now." "All lines visible now.")))
(global-set-key [f6 ?0] 'dv-toggle-visibility)

(defun dv-all-visible ()
"Set the `invisible' property for all characters in buffer to nil."
  (interactive)
  (put-text-property (point-min) (point-max) 'invisible nil)
  (redraw-display)
  (message "No lines marked for invisibility now."))
(global-set-key [f6 ?-] 'dv-all-visible)

_____________________________________________________________________


Perhaps someone else will find it useful.

Regards,
  David V.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]