all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tak Ota <Takaaki.Ota@am.sony.com>
To: <monnier@IRO.UMontreal.CA>
Cc: stephen@xemacs.org, emacs-devel@gnu.org
Subject: Re: collect-string
Date: Fri, 3 Dec 2010 15:15:40 -0800	[thread overview]
Message-ID: <20101203.151540.127977418.Takaaki.Ota@am.sony.com> (raw)
In-Reply-To: <jwvmxommpv6.fsf-monnier+emacs@gnu.org>

Fri, 3 Dec 2010 14:56:51 -0800: Stefan Monnier <monnier@IRO.UMontreal.CA> wrote:

> >> > ! 		  '(0)
> >> > ! 		;; construct a list of subexpression integers
> >> > ! 		(mapcar 'string-to-number
> >> > ! 			(split-string
> >> > ! 			 (let ((default (car occur-collect-submatch-history)))
> >> > ! 			   (read-from-minibuffer
> >> > ! 			    (format "Subexpressions to collect (default %s): " default)
> >> > ! 			    "" nil nil 'occur-collect-submatch-history default)
> >> > ! 			   (car occur-collect-submatch-history)))))
> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >> 
> >> This should be `default', right?
> 
> > No, this is the new input value as read-from-minibuffer prepends this
> > value to the history.
> 
> Oh, right, there's more parens than I thought.  You should use
> `read-string' instead, which will take care of this ugliness.

OK, done.  Indeed its cleaner.

*** ../../../d/pub/emacs/emacs-23.2.90/lisp/replace.el	Thu Dec  2 16:33:19 2010
--- replace.el	Fri Dec  3 15:07:56 2010
***************
*** 527,532 ****
--- 527,535 ----
  Maximum length of the history list is determined by the value
  of `history-length', which see.")
  
+ (defvar occur-collect-regexp-history '("\\1")
+   "History of regexp for occur's collect operation")
+ 
  (defun read-regexp (prompt &optional default-value)
    "Read regexp as a string using the regexp history and some useful defaults.
  Prompt for a regular expression with PROMPT (without a colon and
***************
*** 1028,1037 ****
        (nreverse result))))
  
  (defun occur-read-primary-args ()
!   (list (read-regexp "List lines matching regexp"
! 		     (car regexp-history))
! 	(when current-prefix-arg
! 	  (prefix-numeric-value current-prefix-arg))))
  
  (defun occur-rename-buffer (&optional unique-p interactive-p)
    "Rename the current *Occur* buffer to *Occur: original-buffer-name*.
--- 1031,1055 ----
        (nreverse result))))
  
  (defun occur-read-primary-args ()
!   (let* ((perform-collect (consp current-prefix-arg))
!          (regexp (read-regexp (if perform-collect
!                                   "Collect strings matching regexp"
!                                 "List lines matching regexp")
!                               (car regexp-history))))
!     (list regexp
! 	  (if perform-collect
! 	      ;; Perform collect operation
! 	      (if (zerop (regexp-opt-depth regexp))
! 		  ;; No subexpression so collect the entire match.
! 		  "\\&"
! 		;; Get the regexp for collection pattern.
! 		(let ((default (car occur-collect-regexp-history)))
! 		  (read-string
! 		   (format "Regexp to collect (default %s): " default)
! 		   nil 'occur-collect-regexp-history default)))
! 	    ;; Otherwise normal occur takes numerical prefix argument.
! 	    (when current-prefix-arg
! 	      (prefix-numeric-value current-prefix-arg))))))
  
  (defun occur-rename-buffer (&optional unique-p interactive-p)
    "Rename the current *Occur* buffer to *Occur: original-buffer-name*.
***************
*** 1064,1070 ****
  \\<occur-mode-map>\\[describe-mode] in that buffer will explain how.
  
  If REGEXP contains upper case characters (excluding those preceded by `\\')
! and `search-upper-case' is non-nil, the matching is case-sensitive."
    (interactive (occur-read-primary-args))
    (occur-1 regexp nlines (list (current-buffer))))
  
--- 1082,1099 ----
  \\<occur-mode-map>\\[describe-mode] in that buffer will explain how.
  
  If REGEXP contains upper case characters (excluding those preceded by `\\')
! and `search-upper-case' is non-nil, the matching is case-sensitive.
! 
! When NLINES is a string or when the function is called
! interactively with prefix argument without a number (`C-u' alone
! as prefix) the matching strings are collected into the `*Occur*'
! buffer by using NLINES as a replacement regexp.  NLINES may
! contain \\& and \\N which convention follows `replace-match'.
! For example, providing \"defun\\s +\\(\\S +\\)\" for REGEXP and
! \"\\1\" for NLINES collects all the function names in a lisp
! program.  When there is no parenthesized subexpressions in REGEXP
! the entire match is collected.  In any case the searched buffers
! are not modified."
    (interactive (occur-read-primary-args))
    (occur-1 regexp nlines (list (current-buffer))))
  
***************
*** 1146,1165 ****
      (setq occur-buf (get-buffer-create buf-name))
  
      (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'"
--- 1175,1217 ----
      (setq occur-buf (get-buffer-create buf-name))
  
      (with-current-buffer occur-buf
!       (if (stringp nlines)
! 	  (fundamental-mode) ;; This is for collect opeartion.
! 	(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
! 	       (if (stringp nlines)
!                    ;; Treat nlines as a regexp to collect.
! 		   (let ((bufs active-bufs)
! 			 (count 0))
! 		     (while bufs
! 		       (with-current-buffer (car bufs)
! 			 (save-excursion
! 			   (goto-char (point-min))
! 			   (while (re-search-forward regexp nil t)
!                              ;; Insert the replacement regexp.
! 			     (let ((str (match-substitute-replacement nlines)))
! 			       (if str
! 				   (with-current-buffer occur-buf
! 				     (insert str)
! 				     (setq count (1+ count))
! 				     (or (zerop (current-column))
! 					 (insert "\n"))))))))
!                        (setq bufs (cdr bufs)))
!                      count)
! 		 ;; Perform normal occur.
! 		 (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'"


> >> Hmm... I didn't think of NLINES turning into a list of submatch-numbers,
> >> but that makes me think that maybe we should go one step further and
> >> turn it into a string, like the ones used in replace-match.
> > This is truly brilliant.  Thank you very much.  I love this idea
> > bouncing process.  See below.
> 
> It's not always as successful, sadly.  But yes, it turned out very well
> this time.

As long as people behave rationally with patience and not carried too
much away by own belief it should work fine most of the time.  I'm
glad I haven't been involved in sad cases.

-Tak




  reply	other threads:[~2010-12-03 23:15 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-28 18:56 simple useful functions Tak Ota
2010-10-29  3:39 ` Stephen J. Turnbull
2010-10-29 18:13   ` Tak Ota
2010-10-29 19:02     ` Drew Adams
2010-10-29 19:26       ` Andreas Schwab
2010-10-29 20:19         ` Drew Adams
2010-10-29 20:47           ` Andreas Schwab
2010-10-29 20:56             ` Chad Brown
2010-10-29 21:23               ` Drew Adams
2010-10-30  9:01       ` Stephen J. Turnbull
2010-10-30 10:55     ` Thierry Volpiatto
2010-11-02  0:40     ` Tak Ota
2010-11-02  2:22       ` Stephen J. Turnbull
2010-11-03  0:38         ` Tak Ota
2010-11-03  5:27           ` Stephen J. Turnbull
2010-11-03  8:09             ` Andreas Röhler
2010-11-03 10:13               ` Stephen J. Turnbull
2010-11-03 18:08                 ` Tak Ota
2010-11-03 18:01             ` Tak Ota
2010-11-04  2:10               ` Stephen J. Turnbull
2010-11-04  2:20                 ` Tak Ota
2010-11-04 13:58                 ` collect-string (was: simple useful functions) Stefan Monnier
2010-11-04 18:36                   ` Tak Ota
2010-11-04 20:18                     ` Tak Ota
2010-11-04 20:27                       ` Tak Ota
2010-11-05  7:52                         ` Andreas Röhler
2010-11-08 18:36                     ` collect-string Stefan Monnier
2010-11-09  0:18                       ` collect-string Tak Ota
2010-11-09  9:06                         ` collect-string Stephen J. Turnbull
2010-11-10  2:12                       ` collect-string Tak Ota
2010-11-30  2:14                         ` collect-string Tak Ota
2010-11-30  5:27                           ` collect-string Stephen J. Turnbull
2010-12-02  1:59                             ` collect-string Tak Ota
2010-12-02  7:00                               ` collect-string Stephen J. Turnbull
2010-12-02 14:16                         ` collect-string Stefan Monnier
2010-12-03  1:03                           ` collect-string Tak Ota
2010-12-03 19:17                             ` collect-string Stefan Monnier
2010-12-03 22:31                               ` collect-string Tak Ota
2010-12-03 22:40                                 ` collect-string Davis Herring
2010-12-03 22:47                                   ` collect-string Tak Ota
2010-12-03 22:56                                 ` collect-string Stefan Monnier
2010-12-03 23:15                                   ` Tak Ota [this message]
2010-12-04  2:01                                     ` collect-string Stefan Monnier
2010-12-04  2:07                                       ` collect-string Tak Ota
2010-12-04  3:27                                   ` collect-string Glenn Morris
2010-10-29  8:44 ` simple useful functions Andreas Schwab
2010-11-02  7:27 ` Andreas Röhler
2010-12-03 23:37 ` Tak Ota
2010-12-04  2:36   ` Stefan Monnier
2010-12-04  2:58     ` Tak Ota
2010-12-04  4:36       ` Stefan Monnier
2010-12-04  9:10         ` David Kastrup
2010-12-06 14:08         ` René Kyllingstad
2010-12-06 19:31           ` Stefan Monnier
2010-12-06 18:39         ` Tak Ota
2010-12-06 20:23           ` Stefan Monnier
2010-12-06 21:25             ` Tak Ota
2010-12-07  3:24               ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101203.151540.127977418.Takaaki.Ota@am.sony.com \
    --to=takaaki.ota@am.sony.com \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@IRO.UMontreal.CA \
    --cc=stephen@xemacs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.