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

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

Re: feature request: ability to use occur within *Occur* buffer


From: Ehud Karni
Subject: Re: feature request: ability to use occur within *Occur* buffer
Date: Wed, 7 Mar 2001 22:56:00 +0200

On Thu, 1 Mar 2001 20:11:20 -0700 (MST), Richard Stallman <address@hidden> 
wrote:
> 
>     I think that I've previously used M-x keep-lines (and also M-x
>     flush-lines) in an *Occur* buffer and had it do the right thing.
>     Would it work to just have M-x occur call keep-lines when the buffer
>     it's used on is already an output buffer from an earlier M-x occur?
> 
> It would "work", but I think it is better to do something a little
> more sophisticated which would be "just right".  For instance, it
> should not depend on where point is in the *Occur* buffer and it
> should not be confused by matches against the line number part.
  
OK. I did something, "just" a little more sophisticated.

It does: create a "sub" occur buffer (*Occur-sub*), copy all current
occurrences and select/reject by reg-exp. It also updates the header
line of this sub buffer.

The selection/rejection can be repeated in the *Occur-sub* as needed
(the same buffer is re-used, the original *Occur* is never changed).

It does not do: color the sub-selection (of course you can not color
the sub-rejection).
It works only if `list-matching-lines-default-context-lines' is 0
(which is usually the case).

I propose the keys `r' (reject) and `s' (select) in occur mode for the
these operations.

Ehud.


;;; occur-sub.el - sub-select/reject in occurs buffer
(defun occur-copy-to-sub (cnct regexp)
  "Copy current buffer (must be in `occur-mode') to `*Occur-sub*'
and position on 2nd line (1st occurence line)"
       (or (eq major-mode 'occur-mode)
           (error "Buffer must be in occur mode"))
       (let ((cbf (buffer-substring (point-min) (point-max)))  
             (nbf (get-buffer-create "*Occur-sub*"))
             (str " in buffer ")
              buffer-read-only)
           (occur-copy-local-vars nbf)
           (switch-to-buffer nbf)
           (delete-region (point-min) (point-max))
           (insert cbf)
           (goto-char (point-min))
           (forward-line 1)
           (and (search-backward str nil t)
                (replace-match (concat " " cnct " \""
                                       (regexp-quote regexp)
                                       "\"" str) 'FIX))
           (forward-line 1)))


(defun occur-copy-local-vars (NEW-BUF)
  "Copy all current buffer local vars to NEW-BUF killing all its previuos
local vars (if any). Use `get-buffer-create' to ensure its existence."
       (let ((blv (buffer-local-variables))
             (lmp (current-local-map))
             v-nm v-va)                        ;var name, var-value
           (set-buffer NEW-BUF)
           (kill-all-local-variables)
           (use-local-map lmp)
           (while blv
               (setq v-va (car blv))
               (setq v-nm (car v-va))
               (setq v-va (cdr-safe v-va))
               (condition-case ()
                   (set (make-local-variable v-nm) v-va)
                   (error nil))
               (setq blv (cdr blv)))))


(defun occur-sub-select (regexp)
  "Sub-select occurence from an `occurs' buffer by using `keep-lines'"
       (interactive "sKeep Occurs matching regexp: ")
       (occur-copy-to-sub "and" regexp)
       (let (buffer-read-only)
           (keep-lines regexp)))


(defun occur-sub-reject (regexp)
  "Sub-select occurence from an `occurs' buffer by using `flush-lines'"    
       (interactive "sKeep Occurs matching regexp: ")
       (occur-copy-to-sub "and not" regexp)
       (let (buffer-read-only)
           (flush-lines regexp)))


(define-key occur-mode-map "r" 'occur-sub-reject)  ;reject occurs lines by `r'
(define-key occur-mode-map "s" 'occur-sub-select)  ;select occurs lines by `s'

;;; occur-sub.el ends here


-- 
 @@@@@@ @@@ @@@@@@ @    @   Ehud Karni  Simon & Wiesel  Insurance agency
     @    @      @  @@  @   Tel: +972-3-6212-757    Fax: +972-3-6292-544
     @    @ @    @ @  @@    (USA)  Fax  and  voice  mail:  1-815-5509341
     @    @ @    @ @    @        Better     Safe     Than     Sorry
 http://www.simonwiesel.co.il    mailto:address@hidden



reply via email to

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