From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tak Ota Newsgroups: gmane.emacs.devel Subject: Re: collect-string (was: simple useful functions) Date: Thu, 4 Nov 2010 11:36:54 -0700 Message-ID: <20101104.113654.332196908.Takaaki.Ota@am.sony.com> References: <20101103.110157.309054292.Takaaki.Ota@am.sony.com> <87pqulj0tn.fsf@uwakimon.sk.tsukuba.ac.jp> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1288895898 28500 80.91.229.12 (4 Nov 2010 18:38:18 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 4 Nov 2010 18:38:18 +0000 (UTC) Cc: stephen@xemacs.org, emacs-devel@gnu.org To: Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Nov 04 19:38:13 2010 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PE4hO-0006I4-UQ for ged-emacs-devel@m.gmane.org; Thu, 04 Nov 2010 19:38:12 +0100 Original-Received: from localhost ([127.0.0.1]:55839 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PE4hN-0001qP-JD for ged-emacs-devel@m.gmane.org; Thu, 04 Nov 2010 14:38:09 -0400 Original-Received: from [140.186.70.92] (port=60136 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PE4hH-0001np-OG for emacs-devel@gnu.org; Thu, 04 Nov 2010 14:38:04 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PE4hD-00083g-2R for emacs-devel@gnu.org; Thu, 04 Nov 2010 14:38:03 -0400 Original-Received: from db3ehsobe003.messaging.microsoft.com ([213.199.154.141]:18027 helo=DB3EHSOBE003.bigfish.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PE4hC-00083V-Q0 for emacs-devel@gnu.org; Thu, 04 Nov 2010 14:37:59 -0400 Original-Received: from mail77-db3-R.bigfish.com (10.3.81.240) by DB3EHSOBE003.bigfish.com (10.3.84.23) with Microsoft SMTP Server id 14.1.225.8; Thu, 4 Nov 2010 18:37:56 +0000 Original-Received: from mail77-db3 (localhost.localdomain [127.0.0.1]) by mail77-db3-R.bigfish.com (Postfix) with ESMTP id 42F43B586CA; Thu, 4 Nov 2010 18:37:56 +0000 (UTC) X-SpamScore: -12 X-BigFish: VPS-12(zz1432N98dNzz1202hzzz2fh2a8h61h) X-FB-SS: 0, Original-Received: from mail77-db3 (localhost.localdomain [127.0.0.1]) by mail77-db3 (MessageSwitch) id 1288895827588939_13841; Thu, 4 Nov 2010 18:37:07 +0000 (UTC) Original-Received: from DB3EHSMHS001.bigfish.com (unknown [10.3.81.247]) by mail77-db3.bigfish.com (Postfix) with ESMTP id 8DCCF17C0051; Thu, 4 Nov 2010 18:37:07 +0000 (UTC) Original-Received: from mail7.fw-bc.sony.com (160.33.98.74) by DB3EHSMHS001.bigfish.com (10.3.87.101) with Microsoft SMTP Server (TLS) id 14.1.225.8; Thu, 4 Nov 2010 18:36:59 +0000 Original-Received: from mail1x.bc.in.sel.sony.com ([43.144.65.112]) by mail7.fw-bc.sony.com (Switch-3.4.2/Switch-3.3.2mp) with ESMTP id oA4Iavng023129 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Thu, 4 Nov 2010 18:36:57 GMT Original-Received: from localhost (tak-vaio-z790-043191018003.am.sony.com [43.191.18.3]) by mail1x.bc.in.sel.sony.com (Switch-3.4.2/Switch-3.4.2) with ESMTP id oA4IatYR030847; Thu, 4 Nov 2010 18:36:56 GMT In-Reply-To: X-Mailer: Mew-6.3.50 on Emacs-23.2.1 (i386-mingw-nt6.1.7600 built on 2010-06-30) X-OriginatorOrg: am.sony.com X-detected-operating-system: by eggs.gnu.org: Windows 2000 SP2+, XP SP1+ (seldom 98) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:132369 Archived-At: Thu, 4 Nov 2010 06:58:20 -0700: Stefan Monnier wrote: > >> How about rename the command as collect-occur instead of > >> collect-string and document it as next? > > Wrong namespace. IMO, the name should *start* with occur (but Stefan > > and Yidong are final authorities on that). > > Agreed, namespace cleanliness is one of my favorite forms of > anal retentiveness. > > The way I see it, the suggested collect-string is a variant of occur > where the result buffer contains none of the regexp matches's context. > So it would make sense to integrate it very tightly with `occur', > i.e. make M-x occur do the job of collect-strings for some particular > value of its argument NLINES. Currently, NLINES is assumed to be > a number and all values of that number have a useful meaning, so we'd > have to add this new feature via a non-number value of NLINES. > > E.g. C-u M-x occur could do the collect-string thingy (which is still an > incompatible change since some people may like to use C-u M-x occur to get > 4 lines of context, but you can make omelets without breaking eggs). > > > Stefan > Now I am convinced. How about the change below? In conventional occur zero or negative value for nlines is meaningless correct? We can use that for collection purpose. i.e. C-u 0 M-x occur does the collection of the matching pattern. C-u -1 M-x occur performs the collection of the recorded pattern 1. -Tak (defun occur-1 (regexp nlines bufs &optional buf-name) (unless (and regexp (not (equal regexp ""))) (error "Occur doesn't work with the empty regexp")) (unless buf-name (setq buf-name "*Occur*")) (let (occur-buf (active-bufs (delq nil (mapcar #'(lambda (buf) (when (buffer-live-p buf) buf)) bufs)))) ;; Handle the case where one of the buffers we're searching is the ;; output buffer. Just rename it. (when (member buf-name (mapcar 'buffer-name active-bufs)) (with-current-buffer (get-buffer buf-name) (rename-uniquely))) ;; Now find or create the output buffer. ;; If we just renamed that buffer, we will make a new one here. (setq occur-buf (get-buffer-create buf-name)) (if (or (null (integerp nlines)) (> nlines 0)) ;; nlines is not zero or negative so perform nomal occur (with-current-buffer occur-buf (occur-mode) (let ((inhibit-read-only t) ;; Don't generate undo entries for creation of the initial contents. (buffer-undo-list t)) (erase-buffer) (let ((count (occur-engine regexp active-bufs occur-buf (or nlines list-matching-lines-default-context-lines) (if (and case-fold-search search-upper-case) (isearch-no-upper-case-p regexp t) case-fold-search) list-matching-lines-buffer-name-face nil list-matching-lines-face (not (eq occur-excluded-properties t))))) (let* ((bufcount (length active-bufs)) (diff (- (length bufs) bufcount))) (message "Searched %d buffer%s%s; %s match%s for `%s'" bufcount (if (= bufcount 1) "" "s") (if (zerop diff) "" (format " (%d killed)" diff)) (if (zerop count) "no" (format "%d" count)) (if (= count 1) "" "es") regexp)) (setq occur-revert-arguments (list regexp nlines bufs)) (if (= count 0) (kill-buffer occur-buf) (display-buffer occur-buf) (setq next-error-last-buffer occur-buf) (setq buffer-read-only t) (set-buffer-modified-p nil) (run-hooks 'occur-hook))))) ;; nlines is zero or negative integer perform collect-string (with-current-buffer occur-buf (setq nlines (- nlines)) (fundamental-mode) (let ((inhibit-read-only t) (buffer-undo-list t)) (erase-buffer) (while active-bufs (with-current-buffer (car active-bufs) (save-excursion (goto-char (point-min)) (while (re-search-forward regexp nil t) (let ((str (match-string nlines))) (if str (with-current-buffer occur-buf (insert str) (or (zerop (current-column)) (insert "\n")))))))) (setq active-bufs (cdr active-bufs)))) (display-buffer occur-buf)))))