From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: =?ISO-8859-1?Q?Andreas_R=F6hler?= Newsgroups: gmane.emacs.devel Subject: Re: collect-string (was: simple useful functions) Date: Fri, 05 Nov 2010 08:52:26 +0100 Message-ID: <4CD3B7BA.9070004@online.de> References: <20101104.113654.332196908.Takaaki.Ota@am.sony.com> <20101104.131820.01325185.Takaaki.Ota@am.sony.com> <20101104.132709.29335017.Takaaki.Ota@am.sony.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1288943603 15363 80.91.229.12 (5 Nov 2010 07:53:23 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 5 Nov 2010 07:53:23 +0000 (UTC) Cc: "Stephen J. Turnbull" , Stefan Monnier , Emacs developers To: Tak Ota Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Nov 05 08:53:19 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 1PEH6s-0006JE-9S for ged-emacs-devel@m.gmane.org; Fri, 05 Nov 2010 08:53:18 +0100 Original-Received: from localhost ([127.0.0.1]:44926 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PEH6r-0001NQ-8W for ged-emacs-devel@m.gmane.org; Fri, 05 Nov 2010 03:53:17 -0400 Original-Received: from [140.186.70.92] (port=56834 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PEH6l-0001NL-OJ for emacs-devel@gnu.org; Fri, 05 Nov 2010 03:53:13 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PEH6k-0005uJ-6M for emacs-devel@gnu.org; Fri, 05 Nov 2010 03:53:11 -0400 Original-Received: from moutng.kundenserver.de ([212.227.17.8]:62034) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PEH6j-0005u1-QF for emacs-devel@gnu.org; Fri, 05 Nov 2010 03:53:10 -0400 Original-Received: from noname.home (brln-4dbc5447.pool.mediaWays.net [77.188.84.71]) by mrelayeu.kundenserver.de (node=mreu1) with ESMTP (Nemesis) id 0MaX2T-1Oyg1r0JOj-00Kj5t; Fri, 05 Nov 2010 08:52:50 +0100 User-Agent: Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.1.11) Gecko/20100711 Thunderbird/3.0.6 In-Reply-To: <20101104.132709.29335017.Takaaki.Ota@am.sony.com> X-Provags-ID: V02:K0:IjvsmQgE48Ilw4g2numpBWlxEhDmnpRemGB+YaNZBZa ShdjP1OuIMXE/Cj1u6c58vFkGH0s3scIaYAX3wOQkI4Gad23NY VyuewUjcd7yKOyW31uTMFNLfdG5AbP3upkBYfJcqCzRid1PgsV vLO1VVaOO7szPAsLI9Lv0yJtO4RPQRtygiEOGJcgRROP8vT7wP RiGGC5gEBBGX7wrpi/16FKfeQBN7rbkK9pIQzCLQDk= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. 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:132388 Archived-At: Am 04.11.2010 21:27, schrieb Tak Ota: > And here is the change log update. > > 2010-11-04 Takaaki Ota > > * replace.el (occur-1): Add string collection feature when NLINES > is zero or negative integer value. > > -Tak > > Thu, 04 Nov 2010 13:18:20 -0700: Tak Ota wrote: > >> The documentation can be something like this. I just added the last >> paragraph below. >> >> -Tak >> >> `M-x occur' >> Prompt for a regexp, and display a list showing each line in the >> buffer that contains a match for it. To limit the search to part >> of the buffer, narrow to that part (*note Narrowing::). A numeric >> argument N specifies that N lines of context are to be displayed >> before and after each matching line. Currently, `occur' can not >> correctly handle multiline matches. >> >> The buffer `*Occur*' containing the output serves as a menu for >> finding the occurrences in their original context. Click >> `Mouse-2' on an occurrence listed in `*Occur*', or position point >> there and type; this switches to the buffer that was >> searched and moves point to the original of the chosen occurrence. >> `o' and `C-o' display the match in another window; `C-o' does not >> select it. >> >> After using `M-x occur', you can use `next-error' to visit the >> occurrences found, one by one. *note Compilation Mode::. >> >> When the numeric argument N is 0 or negative the buffer `*Occur*' >> collects all the matched strings. When N is 0 the entire text >> matched is collected. When N is negative the text in the -Nth >> parenthesized expression in the regexp is collected. >> >> >> Thu, 04 Nov 2010 11:36:54 -0700: Tak Ota wrote: >> >>> 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))))) > > > Hi Tak, please permit a consideration, which affects Emacs usability as such: do you recognise the difference between ;;;;;;;;;;;;;;;;;;;;; When the numeric argument N is 0 or negative the buffer `*Occur*' collects all the matched strings. When N is 0 the entire text matched is collected. When N is negative the text in the -Nth parenthesized expression in the regexp is collected. ;;;;;;;;;;;;;;;;;;;; and your original describtion ;;;;;;;;;;;;;;;;;;; "Collect strings of REGEXP (or optional NUM paren) from the current buffer into a collection buffer." ;;;;;;;;;;;;;;;;;;; Which one would you think is more readable? Cheers Andreas