[Top][All Lists]

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

Re: Occur in the *Occur* buffer

From: Ehud Karni
Subject: Re: Occur in the *Occur* buffer
Date: Sat, 2 Jun 2001 13:31:56 +0300

On 01 Jun 2001 19:56:33 -0400, Stefan Monnier 
<monnier+gnu.emacs.bug/news/@RUM.cs.yale.edu> wrote:
> >>>>> "Dan" == Dan Jacobson <address@hidden> writes:
> > First do
> > (occur "string1" nil)
> > Then switch to the *Occur* buffer and do
> > (occur "string2" nil)
> > result:
> > 0 lines matching "string2" in buffer *Occur*.
> Check archives of this list/group (f.ex. with groups.google.com).
> It's a known problem and might be fixed in some later release
> (but probably not in 21.1).

On 2001-03-01 I posted to gnu-emacs-sources some code (repeated below)
that does select and reject in *Occurs* buffer. It is very simplistic
and can be improved in many ways, but it works for the simple cases.
(I myself used it many times since I wrote it).


;;; 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 ")
           (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)
           (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     Mivtach - Simon  Insurance   /"\
 Tel: +972-3-6212-757 Fax: +972-3-6292-544   \ /  ASCII Ribbon Campaign
 (USA) Fax and  voice  mail: 1-815-5509341    X   Against  HTML  Mail
     Better     Safe     Than     Sorry      / \
     mailto:address@hidden    http://www.simonwiesel.co.il

reply via email to

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