[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: collect-string
From: |
Tak Ota |
Subject: |
Re: collect-string |
Date: |
Wed, 1 Dec 2010 17:59:42 -0800 |
Hi Stephen,
Thanks for the comments.
Mon, 29 Nov 2010 21:27:12 -0800: "Stephen J. Turnbull" <address@hidden> wrote:
> Tak Ota writes:
>
> > > ! When prefix argument is provided without a number (`C-u' alone as
> > > ! prefix) the matching strings are collected into the `*Occur*'
> > > ! buffer. When COLLECT is zero the entire matched strings are
> > > ! collected otherwise COLLECTth pair of parenthesized match in the
> > > ! REGEXP is collected."
>
> I don't think this describes the behavior of the prefix argument
> correctly (you will be prompted for a number, IIUC). What happens
> when the prefix argument is a number?
I modified the description slightly.
> > > ! (defun multi-occur-in-matching-buffers (bufregexp regexp &optional
> allbufs collect)
> > > "Show all lines matching REGEXP in buffers specified by BUFREGEXP.
> > > Normally BUFREGEXP matches against each buffer's visited file name,
> > > but if you specify a prefix argument, it matches against the buffer
> name.
>
> The COLLECT argument is undocumented.
I just replicated the same paragraph from occur to multi-occur and
multi-occur-in-matching-buffers below. I added some examples to
demonstrate how this feature is useful for some tasks.
*** ../../../d/pub/emacs/emacs-23.2.90/lisp/replace.el Tue Oct 12 15:43:12 2010
--- replace.el Wed Dec 1 17:55:58 2010
***************
*** 527,532 ****
--- 527,535 ----
Maximum length of the history list is determined by the value
of `history-length', which see.")
+ (defvar last-collect-number 0
+ "The last parenthesized expression number.")
+
(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
***************
*** 1031,1037 ****
(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*.
--- 1034,1044 ----
(list (read-regexp "List lines matching regexp"
(car regexp-history))
(when current-prefix-arg
! (prefix-numeric-value current-prefix-arg))
! (and current-prefix-arg
! (listp current-prefix-arg)
! (setq last-collect-number
! (read-number "Collect" last-collect-number)))))
(defun occur-rename-buffer (&optional unique-p interactive-p)
"Rename the current *Occur* buffer to *Occur: original-buffer-name*.
***************
*** 1050,1056 ****
"*")
(or unique-p (not interactive-p)))))
! (defun occur (regexp &optional nlines)
"Show all lines in the current buffer containing a match for REGEXP.
This function can not handle matches that span more than one line.
--- 1057,1063 ----
"*")
(or unique-p (not interactive-p)))))
! (defun occur (regexp &optional nlines collect)
"Show all lines in the current buffer containing a match for REGEXP.
This function can not handle matches that span more than one line.
***************
*** 1064,1078 ****
\\<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))))
! (defun multi-occur (bufs regexp &optional nlines)
"Show all lines in buffers BUFS containing a match for REGEXP.
This function acts on multiple buffers; otherwise, it is exactly like
`occur'. When you invoke this command interactively, you must specify
! the buffer names that you want, one by one."
(interactive
(cons
(let* ((bufs (list (read-buffer "First buffer to search: "
--- 1071,1105 ----
\\<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 the prefix argument is a list (you type `C-u' alone without
! a number) the matching strings are collected into the `*Occur*'
! buffer. When COLLECT is zero the entire matched strings are
! collected, which is not very useful, otherwise COLLECTth pair of
! parenthesized match in REGEXP is collected. For example,
! providing \"defun \\(\\S +\\)\" for REGEXP and 1 for COLLECT
! collects all the function names in the lisp program. Another
! example, \"<img src=\"\\([^\"]+\\)\" for the REGEXP and 1 for
! COLLECT collects all the image URLs in the HTML file"
(interactive (occur-read-primary-args))
! (occur-1 regexp nlines (list (current-buffer)) nil collect))
! (defun multi-occur (bufs regexp &optional nlines collect)
"Show all lines in buffers BUFS containing a match for REGEXP.
This function acts on multiple buffers; otherwise, it is exactly like
`occur'. When you invoke this command interactively, you must specify
! the buffer names that you want, one by one.
!
! When the prefix argument is a list (you type `C-u' alone without
! a number) the matching strings are collected into the `*Occur*'
! buffer. When COLLECT is zero the entire matched strings are
! collected, which is not very useful, otherwise COLLECTth pair of
! parenthesized match in REGEXP is collected. For example,
! providing \"defun \\(\\S +\\)\" for REGEXP and 1 for COLLECT
! collects all the function names in the lisp program. Another
! example, \"<img src=\"\\([^\"]+\\)\" for the REGEXP and 1 for
! COLLECT collects all the image URLs in the HTML file"
(interactive
(cons
(let* ((bufs (list (read-buffer "First buffer to search: "
***************
*** 1090,1101 ****
(setq ido-ignore-item-temp-list bufs))
(nreverse (mapcar #'get-buffer bufs)))
(occur-read-primary-args)))
! (occur-1 regexp nlines bufs))
! (defun multi-occur-in-matching-buffers (bufregexp regexp &optional allbufs)
"Show all lines matching REGEXP in buffers specified by BUFREGEXP.
Normally BUFREGEXP matches against each buffer's visited file name,
but if you specify a prefix argument, it matches against the buffer name.
See also `multi-occur'."
(interactive
(cons
--- 1117,1139 ----
(setq ido-ignore-item-temp-list bufs))
(nreverse (mapcar #'get-buffer bufs)))
(occur-read-primary-args)))
! (occur-1 regexp nlines bufs nil collect))
! (defun multi-occur-in-matching-buffers (bufregexp regexp &optional allbufs
collect)
"Show all lines matching REGEXP in buffers specified by BUFREGEXP.
Normally BUFREGEXP matches against each buffer's visited file name,
but if you specify a prefix argument, it matches against the buffer name.
+
+ When the prefix argument is a list (you type `C-u' alone without
+ a number) the matching strings are collected into the `*Occur*'
+ buffer. When COLLECT is zero the entire matched strings are
+ collected, which is not very useful, otherwise COLLECTth pair of
+ parenthesized match in REGEXP is collected. For example,
+ providing \"defun \\(\\S +\\)\" for REGEXP and 1 for COLLECT
+ collects all the function names in the lisp program. Another
+ example, \"<img src=\"\\([^\"]+\\)\" for the REGEXP and 1 for
+ COLLECT collects all the image URLs in the HTML file
+
See also `multi-occur'."
(interactive
(cons
***************
*** 1124,1132 ****
(string-match bufregexp
(buffer-file-name
buf))))
buf))
! (buffer-list))))))
! (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
--- 1162,1171 ----
(string-match bufregexp
(buffer-file-name
buf))))
buf))
! (buffer-list)))
! nil collect)))
! (defun occur-1 (regexp nlines bufs &optional buf-name collect)
(unless (and regexp (not (equal regexp "")))
(error "Occur doesn't work with the empty regexp"))
(unless buf-name
***************
*** 1145,1181 ****
;; If we just renamed that buffer, we will make a new one here.
(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'"
! 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)))))))
(defun occur-engine-add-prefix (lines)
(mapcar
--- 1184,1239 ----
;; If we just renamed that buffer, we will make a new one here.
(setq occur-buf (get-buffer-create buf-name))
! (if collect
! (with-current-buffer occur-buf
! (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 collect)))
! (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))
! (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))))))))
(defun occur-engine-add-prefix (lines)
(mapcar
>
> Aside to the maintainers: the ALLBUFS argument could use further
> clarification, but that's not Tak's responsibility.
That's right :)
-Tak